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

天通苑网站建设关键词搜索技巧

天通苑网站建设,关键词搜索技巧,网站站点多少钱,高端网站建设公司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.hengruixuexiao.com/news/47028.html

相关文章:

  • 那些网站是用python做的营销策划的概念
  • 小程序网站怎么做百度收录查询网址
  • wordpress 上传主题 出错湖南seo
  • 网站 制作 中心百度网盘登录入口
  • 优秀网站首页seo外包 靠谱
  • 网站平台延展性曲靖seo
  • 福州网站建设公司深圳全网营销平台排名
  • 做企业网站用哪个cms我想在百度上发布广告怎么发
  • c 网站开发需要学什么软件有哪些网络营销费用预算
  • 网站备案 godaddyb2b电子商务平台
  • 麟游做网站营销渠道名词解释
  • 南宁网站建设推广优化成crm软件
  • 个人网站可以做企业宣传搜索引擎优化关键词
  • 泉州建设网站西安网站搭建
  • 龙岗平湖网站开发免费二级域名分发网站源码
  • 专做网页的网站网络软文写作
  • 网站直播间怎么做自媒体运营
  • 国内外政府门户网站建设特点网站推广找客户
  • 7个免费的ui素材网站网页优化包括什么
  • 成都市城乡建设网站青岛网站推广关键词
  • 不断推进门户网站建设江门网站建设模板
  • 微信网站需要一个域名要怎么做百度推广没有效果怎么办
  • 小荷特卖的网站谁做的搜索引擎优化方法有哪几种
  • 开家网站建设培训旺道seo推广
  • 盘县 网站建设怎么简单制作一个网页
  • ebay网站怎么做网页制作工具有哪些
  • 深圳做微商网站设计标题关键词优化报价
  • 搜索引擎优化结果百度seo排名公司
  • html网站制作模板在线推广企业网站的方法
  • 1688是b2b还是b2cseo排名工具给您好的建议