当前位置: 首页 > news >正文

绍兴网站建设设计网络域名综合查询

绍兴网站建设设计,网络域名综合查询,网站建设逻辑组织的几种模型,9377白蛇传奇leetcode刷题 | 关于二叉树的题型总结3 文章目录leetcode刷题 | 关于二叉树的题型总结3题目连接递增顺序搜索树二叉搜索树中的中序后继把二叉搜索树转换为累加树二叉搜索树迭代器题目连接 897. 递增顺序搜索树 - 力扣(LeetCode) 剑指 Offer II 053. 二…

leetcode刷题 | 关于二叉树的题型总结3

文章目录

  • leetcode刷题 | 关于二叉树的题型总结3
    • 题目连接
    • 递增顺序搜索树
    • 二叉搜索树中的中序后继
    • 把二叉搜索树转换为累加树
    • 二叉搜索树迭代器

题目连接

897. 递增顺序搜索树 - 力扣(LeetCode)

剑指 Offer II 053. 二叉搜索树中的中序后继 - 力扣(LeetCode)

538. 把二叉搜索树转换为累加树 - 力扣(LeetCode)

173. 二叉搜索树迭代器 - 力扣(LeetCode)

递增顺序搜索树

二叉树本身是有序的,可以采用左中右的遍历顺序,使用一个prev节点保存前一个结点

class Solution {TreeNode prev = new TreeNode(-1);TreeNode node = prev;public TreeNode increasingBST(TreeNode root) {dfs(root);return node.right;}public void dfs(TreeNode root){if(root == null) return ;dfs(root.left);prev.right = root;root.left = null;prev = root;dfs(root.right);}
}

二叉搜索树中的中序后继

dfs+中序遍历

class Solution {TreeNode res = null;public TreeNode inorderSuccessor(TreeNode root, TreeNode p) {dfs(root,p);return res;}public TreeNode dfs(TreeNode root,TreeNode p){if(root == null) return null;if(root.val > p.val){res = root;return inorderSuccessor(root.left,p);}else return inorderSuccessor(root.right,p);}
}

使用二分查找到找到cur=p的节点,使用prev记录cur的root节点

然后判断cur节点是否有右子树,如果存在则返会右子树的最左边的节点

如果没有右子树那么直接返会prev,因为pre > cur = p

class Solution {   public TreeNode inorderSuccessor(TreeNode root, TreeNode p) {TreeNode cur = root;TreeNode prev = null;while(cur != p){if(cur.val > p.val){prev = cur;cur = cur.left;}else{cur = cur.right;}}  if(cur.right != null){cur = cur.right;while(cur.left != null){cur = cur.left;}return cur;}return prev;}
}

把二叉搜索树转换为累加树

class Solution {int sum = 0;public TreeNode convertBST(TreeNode root) {if(root == null) return null;convertBST(root.right);root.val += sum; //将当前节点值和大于当前节点值的和相加sum = root.val; convertBST(root.left);return root;}  
}

使用右中左逆序中序遍历的方式并使用栈来存放前一个节点

当cur=null时遍历到了叶子节点,dep.poll() 得到该节点的父节点,将cur = 该父节点

更新cur父节点的val值,题目要求值等于原树中大于或等于 node.val 的值之和,使用sum来保存和

因为使用右中左的遍历顺序,sum始终都是累加

class Solution {public TreeNode convertBST(TreeNode root) {int sum = 0;Deque<TreeNode> deq = new ArrayDeque();      TreeNode cur = root;while(!deq.isEmpty() || cur != null){if(cur != null){deq.push(cur);cur = cur.right;}else{cur = deq.poll();sum += cur.val;cur.val = sum;cur = cur.left;}}return root;}
}

二叉搜索树迭代器

先获得中序遍历结果,然后遍历

class BSTIterator {List<TreeNode> list = null;int index;int siez;public BSTIterator(TreeNode root) {list = new ArrayList<>();index = -1;dfs(root);this.siez = list.size();}public int next() {return list.get(++index).val;}public boolean hasNext() {if (index >= siez-1) return false;return true;}public void dfs(TreeNode root){if (root == null) return ;dfs(root.left);list.add(root);dfs(root.right);}
}

使用栈存入全部的左子节点和根节点

class BSTIterator {Deque<TreeNode> deq = new ArrayDeque<>();public BSTIterator(TreeNode root) {TreeNode node  = root;while (node != null){deq.push(node);node = node.left;}}public int next() {TreeNode cur = deq.poll();if(cur.right != null){TreeNode node = cur.right;while(node != null){deq.push(node);node = node.left;//把所有的左节点都放入deq}}return cur.val;}public boolean hasNext() {return !deq.isEmpty();}
}
http://www.tj-hxxt.cn/news/12820.html

相关文章:

  • 匈牙利网站后缀今日国际重大新闻事件
  • 宝山手机网站制作公司百度广告点击一次多少钱
  • 临沂网站建设网站推广百度云网盘登录入口
  • 佛山做网站优化各种资源都有的搜索引擎
  • 企业网站怎么做优化百度广告销售
  • 太原小店区疫情最新通告seo引擎优化
  • 平面设计师推荐网站爱站网官网
  • 网站开发公司的销售方式手机清理优化软件排名
  • 濮阳网站建设在哪做最新新闻热点素材
  • 上海可靠的网站建设公司cnzz
  • 贵州省电力建设施工企业商会游戏优化软件
  • 网站jsp充值和体现系统怎么做看啥网一个没有人工干预的网
  • 优秀专题网站seo推广系统
  • 网站开发分类2023年第三波疫情9月
  • 南通网站建设推广站内关键词排名软件
  • dw博客网站怎么做竞价代运营公司哪家好
  • 企业招聘官网哈尔滨seo
  • 网站上线 flashcom域名多少钱一年
  • 建设网站需要的编程吸引人的营销标题
  • 河北汉佳 做网站的公司老鬼seo
  • 网站收索功能怎么做拉新app推广平台
  • 百度网盟网站有哪些网络推广是网络营销的基础
  • 内部网站建设党委网络优化app哪个好
  • 书画院网站建设方案百度提交入口地址在哪
  • 人大网站建设报价单东莞seo整站优化火速
  • 怎么做网站或APP软文模板300字
  • 做网站有哪些要求seo关键词如何设置
  • 网站建设主要工作内容电商平台网站
  • 民治网站设计圳网站建设公司百度app最新版本
  • 广州网站设计教程网站建设网络推广seo