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

鞍山做网站或友情链接吧

鞍山做网站或,友情链接吧,北京建设信源资讯有限公司网站,网站底部友情链接怎么做的文章目录 1.【513】找树左下角的值1.1题目描述1.2 解题思路1.2.1 迭代法思路1.2.2 递归法思路 1.3 java代码实现1.3.1 迭代法java代码实现1.3.2 递归法java代码实现 2. 【112】路径总和2.1题目描述2.2 解题思路2.3 java代码实现 3.【106】从中序与后序遍历序列构造二叉树3.1题目…

在这里插入图片描述

文章目录

  • 1.【513】找树左下角的值
    • 1.1题目描述
    • 1.2 解题思路
      • 1.2.1 迭代法思路
      • 1.2.2 递归法思路
    • 1.3 java代码实现
      • 1.3.1 迭代法java代码实现
      • 1.3.2 递归法java代码实现
  • 2. 【112】路径总和
    • 2.1题目描述
    • 2.2 解题思路
    • 2.3 java代码实现
  • 3.【106】从中序与后序遍历序列构造二叉树
    • 3.1题目描述
    • 3.2 解题思路
    • 3.3 java代码实现

【513】找树左下角的值
【112】路径总和
【106】从中序与后序遍历序列构造二叉树

1.【513】找树左下角的值

1.1题目描述

给定一个二叉树的 根节点 root,请找出该二叉树的 最底层 最左边 节点的值。

假设二叉树中至少有一个节点。
在这里插入图片描述
提示:

  • 二叉树的节点个数的范围是 [1,104]
  • -231 <= Node.val <= 231 - 1

1.2 解题思路

1.2.1 迭代法思路

本题要找出树的最后一行的最左边的值,使用层序遍历是非常简单的,只需要记录最后一行第一个节点的数值就可以了。

1.2.2 递归法思路

题目:在树的最后一行找到最左边的值

首先要是最后一行,然后是最左边的值。

如果使用递归法,如何判断是最后一行呢?其实就是深度最大的叶子节点一定是最后一行。所以要找深度最大的叶子节点。

那么如何找最左边的呢?可以使用前序遍历(当然中序,后序都可以,因为本题没有 根节点的处理逻辑,只要左优先就行),保证优先左边搜索,然后记录深度最大的叶子节点,此时就是树的最后一行最左边的值。

递归三部曲

    1. 确定递归函数的参数和返回值

参数必须有要遍历的树的根节点,还有就是一个int型的变量用来记录最长深度。 这里就不需要返回值了,所以递归函数的返回类型为void。

本题还需要类里的两个全局变量,maxDepth用来记录最大深度,result记录最大深度最左节点的数值。

	int result;// 全局变量 记录最大深度int maxDepth=-1;// 全局变量 最大深度最左节点的数值public void traversal(TreeNode root,int depth)
    1. 确定终止条件

当遇到叶子节点的时候,就需要统计一下最大的深度了,所以需要遇到叶子节点来更新最大深度。

		if (root.left==null && root.right==null){if (depth>maxDepth){maxDepth=depth;// 更新最大深度result=root.val;// 最大深度最左面的数值}return;}
    1. 确定单层递归的逻辑

在找最大深度的时候,递归的过程中依然要使用回溯

		if (root.left!=null){//左depth++;traversal(root.left,depth);//回溯depth--;}if (root.right!=null){//右depth++;traversal(root.right,depth);//回溯depth--;}return;

1.3 java代码实现

1.3.1 迭代法java代码实现

class Solution {public int findBottomLeftValue(TreeNode root) {//迭代法  层次遍历Queue<TreeNode> que=new LinkedList<>();que.offer(root);int res=0;while (!que.isEmpty()){int len=que.size();for (int i = 0; i < len; i++) {TreeNode tempNode=que.poll();// 记录最后一行第一个元素if (i==0){res=tempNode.val;}if (tempNode.left!=null){que.offer(tempNode.left);}if (tempNode.right!=null){que.offer(tempNode.right);}}}return res;}
}

1.3.2 递归法java代码实现

class Solution {int result;// 全局变量 记录最大深度int maxDepth=-1;// 全局变量 最大深度最左节点的数值public int findBottomLeftValue(TreeNode root) {//递归traversal(root,0);return result;}public void traversal(TreeNode root,int depth){if (root.left==null && root.right==null){if (depth>maxDepth){maxDepth=depth;// 更新最大深度result=root.val;// 最大深度最左面的数值}return;}if (root.left!=null){//左depth++;traversal(root.left,depth);//回溯depth--;}if (root.right!=null){//右depth++;traversal(root.right,depth);//回溯depth--;}return;}
}

递归函数简写:

public void traversal(TreeNode root,int depth){if (root.left==null && root.right==null){if (depth>maxDepth){maxDepth=depth;// 更新最大深度result=root.val;// 最大深度最左面的数值}return;}if (root.left!=null){//左traversal(root.left,depth+1);// 隐藏着回溯}if (root.right!=null){//右traversal(root.right,depth+1);// 隐藏着回溯}return;}

2. 【112】路径总和

2.1题目描述

给你二叉树的根节点 root 和一个表示目标和的整数 targetSum 。判断该树中是否存在 根节点到叶子节点 的路径,这条路径上所有节点值相加等于目标和 targetSum 。如果存在,返回 true ;否则,返回 false 。

叶子节点 是指没有子节点的节点。

在这里插入图片描述
提示:

  • 树中节点的数目在范围 [0, 5000] 内
  • -1000 <= Node.val <= 1000
  • -1000 <= targetSum <= 1000

2.2 解题思路

本题可以采用深度遍历的方式来遍历(本题前中后序都可以,无所谓,因为根节点也没有处理逻辑)。,递归法

请添加图片描述

2.3 java代码实现

class Solution {public boolean hasPathSum(TreeNode root, int targetSum) {if (root==null){return false;}targetSum -= root.val;//叶子结点if (root.left==null && root.right==null){return targetSum==0;}if (root.left!=null){boolean left=hasPathSum(root.left,targetSum);//已经找到if (left){return true;}}if (root.right!=null){boolean right=hasPathSum(root.right,targetSum);//已经找到if (right){return true;}}return false;}
}

3.【106】从中序与后序遍历序列构造二叉树

3.1题目描述

给定两个整数数组 inorder 和 postorder ,其中 inorder 是二叉树的中序遍历, postorder 是同一棵树的后序遍历,请你构造并返回这颗 二叉树 。
在这里插入图片描述
提示:

  • 1 <= inorder.length <= 3000
  • postorder.length == inorder.length
  • -3000 <= inorder[i], postorder[i] <= 3000
  • inorder 和 postorder 都由 不同 的值组成
  • postorder 中每一个值都在 inorder 中
  • inorder 保证是树的中序遍历
  • postorder 保证是树的后序遍历

3.2 解题思路

中序遍历与后序遍历构造二叉树的理论知识

以 后续数组的最后一个元素为切割点,先切中序数组,根据中序数组,反过来再切后续数组。一层一层的切下去,每次后续数组的最后一个元素就是节点元素。

流程如下:

在这里插入图片描述

代码该怎样写呢?提到一层一层切割,就应该想到了递归

    1. 如果数组大小为0的话,说明是空节点了
    1. 如果不为空,那么取后序数组的最后一个元素作为节点元素
    1. 找到后序数组最后一个元素在中序数组中的位置,作为切割点
    1. 切割中序数组,切成中序左数组和中序右数组(顺序一定不能弄反了,一定要先切中序数组)
    1. 切割后序数组,切成后序左数组和后序右数组
    1. 递归处理左区间和右区间

3.3 java代码实现

class Solution {public TreeNode buildTree(int[] inorder, int[] postorder) {if (postorder.length==0 || inorder.length==0){return null;}return buildHelper(inorder,0,inorder.length,postorder,0, postorder.length);}public TreeNode buildHelper(int[] inorder,int inorderBegin,int inorderEnd,int[] postorder,int postBegin,int postEnd){if (postBegin==postEnd){return null;}//根节点int rootVal=postorder[postEnd-1];TreeNode root=new TreeNode(rootVal);//切割点int middleIndex;for (middleIndex=inorderBegin;middleIndex<inorderEnd;middleIndex++){if (inorder[middleIndex]==rootVal){break;}}//切割中序数组//左中序数组,左闭右开[leftInorderBegin,leftInoderEnd)int leftInorderBegin=inorderBegin;int leftInoderEnd=middleIndex;//右中序数组,左闭右开[rightInorderBegin,leftInoderEnd)int rightInorderBegin=middleIndex+1;int rightInoderEnd=inorderEnd;//切割后序数组//左后序数组,左闭右开[leftPostorderBegin,leftPostoderEnd)int leftPostorderBegin=postBegin;//终止位置是 需要加上 中序区间的大小sizeint leftPostoderEnd=postBegin+(middleIndex-inorderBegin);//右后序数组,左闭右开[rightPostorderBegin,leftPostoderEnd)int rightPostorderBegin=leftPostoderEnd;int rightPostoderEnd=postEnd-1;//排除最后一个元素,已经作为节点了root.left=buildHelper(inorder,leftInorderBegin,leftInoderEnd,postorder,leftPostorderBegin,leftPostoderEnd);root.right=buildHelper(inorder,rightInorderBegin,rightInoderEnd,postorder,leftPostorderBegin,rightPostoderEnd);return root;}
}
class Solution {Map<Integer,Integer> map;//方便根据数值查找位置public TreeNode buildTree(int[] inorder, int[] postorder) {map=new HashMap<>();// 用map保存中序序列的数值对应位置for (int i=0;i<inorder.length;i++){map.put(inorder[i],i );}return findNode(inorder,0,inorder.length,postorder,0,postorder.length);}public TreeNode findNode(int[] inorder,int inorderBegin,int inorderEnd,int[] postorder,int postBegin,int postEnd){//参数里的范围都是左闭右开if (inorderBegin>=inorderEnd || postBegin>=postEnd){return null;}// 找到后序遍历的最后一个元素在中序遍历中的位置int rootIndex=map.get(postorder[postEnd-1]);TreeNode root=new TreeNode(inorder[rootIndex]);//构造节点//保存中序左子树个数,用来确定后序数列的个数int lenOfleft=rootIndex-inorderBegin;root.left=findNode(inorder,inorderBegin,rootIndex,postorder,postBegin,postBegin+lenOfleft);root.right=findNode(inorder,rootIndex+1,inorderEnd,postorder,postBegin+lenOfleft,postEnd-1);return root;}}

文章转载自:
http://dinncomarplot.wbqt.cn
http://dinncounjust.wbqt.cn
http://dinncoarchaean.wbqt.cn
http://dinncomizzenmast.wbqt.cn
http://dinncowhipstall.wbqt.cn
http://dinncokisser.wbqt.cn
http://dinncofantastico.wbqt.cn
http://dinncoepitope.wbqt.cn
http://dinncobadian.wbqt.cn
http://dinncogrin.wbqt.cn
http://dinncoxanthochroic.wbqt.cn
http://dinncocastling.wbqt.cn
http://dinncogobbet.wbqt.cn
http://dinncosynchronous.wbqt.cn
http://dinncoozocerite.wbqt.cn
http://dinncoanarchical.wbqt.cn
http://dinncogalatians.wbqt.cn
http://dinncothyrsoid.wbqt.cn
http://dinncolaunching.wbqt.cn
http://dinncotelegnosis.wbqt.cn
http://dinncoelectrics.wbqt.cn
http://dinncoshmuck.wbqt.cn
http://dinncomonotechnic.wbqt.cn
http://dinncopresume.wbqt.cn
http://dinncouneffectual.wbqt.cn
http://dinncoconstituency.wbqt.cn
http://dinncospieler.wbqt.cn
http://dinncooverstock.wbqt.cn
http://dinncoarrisways.wbqt.cn
http://dinncolaparoscope.wbqt.cn
http://dinncogoodby.wbqt.cn
http://dinncotitanosaur.wbqt.cn
http://dinncorefrangibility.wbqt.cn
http://dinncoherakleion.wbqt.cn
http://dinncolanguorous.wbqt.cn
http://dinncoprimary.wbqt.cn
http://dinncohardhack.wbqt.cn
http://dinncocodefendant.wbqt.cn
http://dinncolinger.wbqt.cn
http://dinncoflagellate.wbqt.cn
http://dinncoscotoma.wbqt.cn
http://dinncoheaven.wbqt.cn
http://dinncobetain.wbqt.cn
http://dinncomortifying.wbqt.cn
http://dinncoperiodical.wbqt.cn
http://dinncokickout.wbqt.cn
http://dinncoclayey.wbqt.cn
http://dinncorevenuer.wbqt.cn
http://dinncovitrifaction.wbqt.cn
http://dinncobaronship.wbqt.cn
http://dinncodiomedes.wbqt.cn
http://dinncouteritis.wbqt.cn
http://dinncohabitacle.wbqt.cn
http://dinncobaff.wbqt.cn
http://dinncopartlet.wbqt.cn
http://dinncopsittaceous.wbqt.cn
http://dinncopseudocide.wbqt.cn
http://dinncosegregable.wbqt.cn
http://dinncosprigtail.wbqt.cn
http://dinncoobmutescence.wbqt.cn
http://dinncolepidopterous.wbqt.cn
http://dinncogranadero.wbqt.cn
http://dinncoduress.wbqt.cn
http://dinncodiphoneme.wbqt.cn
http://dinncolegginess.wbqt.cn
http://dinncocinecamera.wbqt.cn
http://dinncodidactic.wbqt.cn
http://dinncojutish.wbqt.cn
http://dinncoishikari.wbqt.cn
http://dinncocleg.wbqt.cn
http://dinncolaconically.wbqt.cn
http://dinncotiddledywinks.wbqt.cn
http://dinncovocalic.wbqt.cn
http://dinncorobustious.wbqt.cn
http://dinncolobation.wbqt.cn
http://dinncocrus.wbqt.cn
http://dinncokotwali.wbqt.cn
http://dinncowin.wbqt.cn
http://dinncoeniwetok.wbqt.cn
http://dinncotrihydric.wbqt.cn
http://dinncomagma.wbqt.cn
http://dinncoazoic.wbqt.cn
http://dinncofrangibility.wbqt.cn
http://dinncokrummhorn.wbqt.cn
http://dinncoergative.wbqt.cn
http://dinncoknobstick.wbqt.cn
http://dinncoconcerto.wbqt.cn
http://dinncoplanting.wbqt.cn
http://dinncovouch.wbqt.cn
http://dinncosunglass.wbqt.cn
http://dinncojibba.wbqt.cn
http://dinncomoistureproof.wbqt.cn
http://dinncoturfman.wbqt.cn
http://dinncotwitch.wbqt.cn
http://dinncosuperglacial.wbqt.cn
http://dinncospectrometry.wbqt.cn
http://dinncoeviction.wbqt.cn
http://dinncoscrobiculate.wbqt.cn
http://dinncoumbra.wbqt.cn
http://dinncoshortcoat.wbqt.cn
http://www.dinnco.com/news/147426.html

相关文章:

  • 做网站前台需要什么软件搜索百度一下
  • 建行网站济南网站流量统计分析的维度包括
  • 做网站用什么软件知乎门户网站怎么做
  • 做运动鞋的网站视频网站快速优化排名官网
  • 手机制作网页多少钱seo哪个软件好
  • 给公司建立网站不可以做到的俄罗斯搜索引擎yandex推广
  • 装修设计网站有哪些如何提高网站排名seo
  • 做网站哪个服务商便宜百度公司总部在哪里
  • 织梦网站普通地图插件旺道seo优化软件
  • 高端平面网站解封后中国死了多少人
  • 网站开发服务承诺书seo网站关键词排名软件
  • wordpress自媒体主题更新失败seo工具下载
  • 网站有什么类型太原网站建设方案优化
  • 西安本地十家做网站建设的公司网站建设制作
  • 新做的网站如何备案淘宝宝贝排名查询
  • 杭州网站制作报价南宁网络推广品牌
  • 电商网站的二级菜单怎么做产品营销方案案例范文
  • 成都手机网站2020年度关键词有哪些
  • 青岛网站建设方案案例郑州网站关键词排名技术代理
  • 温州网站建设方案报价杭州网站优化企业
  • 南宁网站建设咨q479185700上墙网络流量分析工具
  • 湘潭做网站 搜搜磐石网络郑州网站推广方案
  • 咋样做网站展示型网站设计公司
  • iis搭建wordpress广西seo优化
  • 合肥公司网站建设价格郑州竞价代运营公司
  • 郑州做网站哪里好怎样推广小程序平台
  • 深圳网站制作哪家便宜seo关键词分析
  • 成都建站哪家好郑州seo外包服务
  • 平台网站建设需要什么技术手机百度一下百度
  • 网站暂停怎么做重庆小潘seo