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

真实免费建站平台百度推广个人能开户吗

真实免费建站平台,百度推广个人能开户吗,网站技术建设维护技术论文,病毒共享WordPress主题代码随想录day16| 找树左下角的值 、 路径总和 、 从中序与后序遍历序列构造二叉树 513找树左下角的值层序遍历法递归法 路径总和112. 路径总和113. 路径总和 II 从中序与后序遍历序列构造二叉树思路 513找树左下角的值 层序遍历法 使用层序遍历,找到最后一层最左边…

代码随想录day16| 找树左下角的值 、 路径总和 、 从中序与后序遍历序列构造二叉树

  • 513找树左下角的值
    • 层序遍历法
    • 递归法
  • 路径总和
    • 112. 路径总和
    • 113. 路径总和 II
  • 从中序与后序遍历序列构造二叉树
    • 思路

513找树左下角的值

层序遍历法

使用层序遍历,找到最后一层最左边的数值(每层循环的第一个值)返回即可

class Solution {public int findBottomLeftValue(TreeNode root) {int res = 0;Queue<TreeNode> queue = new LinkedList();queue.offer(root);while(!queue.isEmpty()){int size = queue.size();for(int i = 0 ; i < size ; i++){TreeNode node = queue.poll();if(i == 0){res = node.val;}if(node.left!= null){queue.offer(node.left);}if(node.right != null){queue.offer(node.right);}}} return res;}}

递归法

使用递归遍历二叉树并且记录当前深度,到根节点时更新最大深度的值,然后使用回溯来更新其他路径时的深度

class Solution {int maxDeepth = -1;int res = 0;public int findBottomLeftValue(TreeNode root) {int deepth = 0;getdeepthleft(root, deepth);return res;}public void getdeepthleft(TreeNode root, int deepth){if(root.left == null && root.right == null){if(deepth > maxDeepth){maxDeepth = deepth;res = root.val;return ;}return ;}if(root.left != null){deepth += 1;getdeepthleft(root.left, deepth);deepth -= 1;}if(root.right != null){deepth += 1;getdeepthleft(root.right, deepth);deepth -= 1;}}
}

路径总和

112. 路径总和

使用递归加回溯找出是否有符合目标的路径

class Solution {public boolean hasPathSum(TreeNode root, int targetSum) {if(root == null){return false;}int sum = 0;return getPath(root, sum, targetSum);}public boolean getPath(TreeNode root, int sum, int targetSum){if(root.left == null && root.right == null){if(targetSum == sum + root.val){return true;}return false;}sum += root.val;boolean left = false;boolean right = false;if(root.left != null){left = getPath(root.left, sum, targetSum);}if(root.right != null){right = getPath(root.right, sum, targetSum);}if(left || right){return true; }else{sum -= root.val;return false;}}
}

113. 路径总和 II

这里相比上一个题目需要收集具体的符合目标的路径,所以回溯两个值:当前路径值的和(sum )、当前收集的路径节点(path )

class Solution {List<Integer> path = new ArrayList();List<List<Integer>> paths = new ArrayList();public List<List<Integer>> pathSum(TreeNode root, int targetSum) {if(root == null){return paths;}int sum = 0;getPath(root, sum, targetSum);return paths;}public void getPath(TreeNode root, int sum, int targetSum){path.add(root.val);sum += root.val;if(root.left == null && root.right == null){if(targetSum == sum){//这里注意新创建一个列表,不能直接存原列表,因为原列表后序会修改paths.add(new ArrayList(path));}return ;}if(root.left != null){getPath(root.left, sum, targetSum);path.remove(path.size()-1);}if(root.right != null){getPath(root.right, sum, targetSum);path.remove(path.size()-1);}}}

从中序与后序遍历序列构造二叉树

思路

具体思路

注意:切割数组时的边界处理

class Solution {public TreeNode buildTree(int[] inorder, int[] postorder) {return getTree(inorder, postorder);}public TreeNode getTree(int[] inorder, int[] postorder) {if(inorder == null || postorder == null){return null;}int val = postorder[postorder.length-1];TreeNode root = new TreeNode(val);if(inorder.length == 1){return root;}int i = 0;for(;i < inorder.length ; i++){if(inorder[i] == val){break;}}int[] inorderLeft = null;int[] inorderRight = null;if(i > 0){inorderLeft = new int[i];for(int m = 0 ; m < i ; m++){inorderLeft[m] = inorder[m];}}if(i < inorder.length-1){inorderRight = new int[inorder.length-i-1];int n = 0;for(int m = i+1 ; m < inorder.length ; m++){inorderRight[n++] = inorder[m];}}int j = 0;int[] postorderLeft = null;int[] postorderRight = null;if(inorderLeft != null){postorderLeft = new int[inorderLeft.length];for(int m = 0 ; m < inorderLeft.length ; m++){postorderLeft[m] = postorder[m];}}if(inorderRight!=null){postorderRight = new int[inorderRight.length];int n = 0;int m = 0;if(inorderLeft != null){m = inorderLeft.length;}for(; m < postorder.length-1 ; m++){postorderRight[n] = postorder[m];System.out.print(postorderRight[n] + " ");n++;}}root.left = getTree(inorderLeft, postorderLeft);root.right = getTree(inorderRight, postorderRight);return root;}}
http://www.zhongyajixie.com/news/6039.html

相关文章:

  • 泰安网站建设公司排名长沙网站优化对策
  • 自己做网站好做吗如何做百度竞价推广
  • 网站开发语言怎么选seo推广软件排行榜前十名
  • 自己做企业网站可以吗长春网站建设路
  • 单页网站 seo站长工具排名查询
  • 做网站公司哪家公司好营销策划精准营销
  • 阐述电子商务网站的建设要求qq营销推广方法和手段
  • 嘉兴教育网站建设山西网络推广专业
  • 网站建设与管理需要什么软件北京seo管理
  • 仿制网站软件淘宝店铺转让价格表
  • 文广网站建设各大搜索引擎提交入口
  • 淘宝客 网站备案经典软文广告案例
  • 有哪些做政府网站的相关公司怎么找精准客户资源
  • 5000元网站seo推广推广一次多少钱
  • 网站搜索引擎优化的基本内容数据分析一般用什么软件
  • 怎样用网站做淘宝推广昆明网络推广方式有哪些
  • 网站建设需要多长时间百度百科官网入口
  • 哪些网站可以做养殖的广告国内新闻大事20条
  • 网站建设的销售渠道sem推广
  • 域名停靠app网站入口重庆seo服务
  • 昆明网站推广专员搜索引擎营销是什么
  • 公司网站工商备案怎么做拓客软件
  • 广 做网站蓝光电影下载100种找客户的方法
  • 博山区住房和城乡建设局网站全球十大搜索引擎排名
  • 国外网站要备案吗推广网站哪个好
  • bootstrap中文网站模板拼多多关键词优化是怎么弄的
  • 四川省建行网站百度推广一年多少钱
  • 苏州seo网站优化软件友情链接可以随便找链接加吗
  • 通过高新区网站建设seo网站分析
  • 房山网站建设公司网络营销推广软件