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

宁波手机网站建设做网站的项目开发计划书

宁波手机网站建设,做网站的项目开发计划书,标书制作软件免费版,wordpress写文章分段leetcode刷题 | 关于二叉树的题型总结3 文章目录leetcode刷题 | 关于二叉树的题型总结3题目连接递增顺序搜索树二叉搜索树中的中序后继把二叉搜索树转换为累加树二叉搜索树迭代器题目连接 897. 递增顺序搜索树 - 力扣#xff08;LeetCode#xff09; 剑指 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);} }使用二分查找到找到curp的节点使用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;} }使用右中左逆序中序遍历的方式并使用栈来存放前一个节点 当curnull时遍历到了叶子节点dep.poll() 得到该节点的父节点将cur 该父节点 更新cur父节点的val值题目要求值等于原树中大于或等于 node.val 的值之和使用sum来保存和 因为使用右中左的遍历顺序sum始终都是累加 class Solution {public TreeNode convertBST(TreeNode root) {int sum 0;DequeTreeNode 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 {ListTreeNode 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 {DequeTreeNode 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.eeditor.cn/news/119151/

相关文章:

  • 微网站建设找哪家公司天眼查官网查询入口
  • 自学网站编程江苏最新消息今天
  • 网站搭建联系方式知名做网站
  • 龙岩做网站哪家好seo研究中心晴天
  • 两个网站如何使用一个虚拟主机商场vi设计 知名公司
  • 衡阳电商网站建设国家最新政策
  • 做网站的准备什么软件wordpress如何修改行距
  • 动态做网站网络开发公司简介
  • 哪些网站是php网站自建
  • 南京哪里可以做网站陕西省建设厅网站月报
  • 单位外部网站建设价格吉林省吉林市区号
  • 环保推广哪个网站好网页设计怎么运行
  • 海南平台网站建设企业网页设计师培训宣传语
  • 资阳房产网站建设益阳网站开发
  • 玉林做网站wordpress分类默认模板
  • html5教程 pdf为企业做好服务优化营商环境
  • 网站怎么做支付深圳建站软件
  • 如何建设网站的外链无锡网站建设公司哪家好
  • 高校档案馆网站建设凡科网站怎么建设个人网站
  • 介绍网站建设鄂尔多斯做网站
  • 网站后台无法修改信息网站上线前的准备
  • 泰康人寿网站如何做计划领取wordpress首页背景
  • 南京网站建设网苏州网站建设布局
  • 建设厅网站账号密码忘记怎么办网络公司做网站赚钱码
  • 素材网站定制做网站先要学
  • 个人网站备案资料淮南市城乡建设局网站
  • 厦门仿站定制模板建站杭州网站建设技术
  • wex5做视频网站网站的二级导航怎么做
  • 波莱网站开发查询网站服务器类型
  • 佟年为韩商言做的网站2023新闻大事10条