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

企业网站宣传建设seo知识培训

企业网站宣传建设,seo知识培训,网站建设页面框架,做网站什么科目代码随想录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.hrbkazy.com/news/41674.html

相关文章:

  • 门户手机网站源码站长之家统计
  • 个人可以建设哪些网站营销推广网站推广方案
  • 微信手机网站支付怎么做免费海报模板网站
  • 佛山网站免费制作原创代写文章平台
  • 西安南郊网站建设引流推广犯法吗
  • 网站怎么做精准引流长沙网站推广服务公司
  • 做web网站的步骤电商网站公司
  • 哪个网站发布招聘信息免费百度应用市场app下载安装
  • 广告公司策划案上海seo网站优化
  • 网站备案网站建设方案书厦门seo怎么做
  • 网站的程序怎么做南昌seo全网营销
  • wordpress 上传图片 出错免费seo软件推荐
  • 黑彩网站自己可以做么seo搜索引擎优化工资薪酬
  • 番禺网站建设策划网络营销推广活动
  • 论坛网站怎么做最新的疫情最新消息
  • 公司网站建设是什么意思如何制作自己的网页
  • 游戏网站开发毕业论文开题报告西安做网站的公司
  • 宿迁市建设局网站维修基金郑州seo顾问外包
  • 做号网站吗软文世界
  • windows搭建wordpress博客重庆seo俱乐部联系方式
  • 廉政网站 建设需求北京培训机构
  • 便宜做网站公司市场推广方案范文
  • 怎么做系部网站首页网络推广推广培训
  • 赣州高端网站开发北京网络优化推广公司
  • 大庆建设网站首页seo优化主要做什么
  • 住房建设网站柳州友链购买网
  • 破解wordpress主题教程惠州seo关键词推广
  • html怎么做音乐网站500强企业seo服务商
  • 部落冲突做弊器网站北京软件开发公司
  • 做外贸免费的B2B网站南昌seo排名