题目信息 513. 找树左下角的值
- 题目链接: https://leetcode.cn/problems/find-bottom-left-tree-value/description/
- 题目描述:
给定一个二叉树的 根节点root,请找出该二叉树的 最底层 最左边 节点的值。
假设二叉树中至少有一个节点。
解法: {{递归}}
解题思路
在树的最后一行找到最左边的值。
首先要是最后一行,然后是最左边的值。
如果使用递归法,如何判断是最后一行呢,其实就是深度最大的叶子节点一定是最后一行。
如果对二叉树深度和高度还有点疑惑的话,请看:110.平衡二叉树 (opens new window)。
所以要找深度最大的叶子节点。
那么如何找最左边的呢?可以使用前序遍历(当然中序,后序都可以,因为本题没有 中间节点的处理逻辑,只要左优先就行),保证优先左边搜索,然后记录深度最大的叶子节点,此时就是树的最后一行最左边的值。
递归三部曲:
- 确定递归函数的参数和返回值
参数必须有要遍历的树的根节点,还有就是一个int型的变量用来记录最长深度。 这里就不需要返回值了,所以递归函数的返回类型为void。
本题还需要类里的两个全局变量,maxLen用来记录最大深度,result记录最大深度最左节点的数值。
代码如下:
private int maxDepth = -1; // 全局变量 记录最大深度
private int value = 0; // 全局变量 最大深度最左节点的数值
public int traversal(TreeNode root,int depth)
- 确定终止条件
当遇到叶子节点的时候,就需要统计一下最大的深度了,所以需要遇到叶子节点来更新最大深度。
代码如下:
if (root.left == null && root.right == null){
if (depth > maxDepth){
maxDepth = depth; // 更新最大深度
value = root.val; // 最大深度最左面的数值
}
}
- 确定单层递归的逻辑
在找最大深度的时候,递归的过程中依然要使用回溯,代码如下:
// 中
if (root.left != null){ // 左
depth++; // 深度加一
traversal(root.left,depth);
depth--; // 回溯,深度减一
}
if (root.right != null){ // 右
depth++; // 深度加一
traversal(root.right,depth);
depth--; // 回溯,深度减一
}
return value;
完整代码如下:
代码实现
public int findBottomLeftValue(TreeNode root){
value = root.val;
traversal(root,0);
return value;
}
private int maxDepth = -1;
private int value = 0;
public int traversal(TreeNode root,int depth){
if (root == null) return 0;
if (root.left == null && root.right == null){
if (depth > maxDepth){
maxDepth = depth;
value = root.val;
}
}
if (root.left != null){
depth++;
traversal(root.left,depth);
depth--;
}
if (root.right != null){
depth++;
traversal(root.right,depth);
depth--;
}
return value;
}
题目信息 112. 路径总和
- 题目链接: https://leetcode.cn/problems/path-sum/description/
- 题目描述:
给你二叉树的根节点root和一个表示目标和的整数targetSum。判断该树中是否存在 根节点到叶子节点 的路径,这条路径上所有节点值相加等于目标和targetSum。如果存在,返回true;否则,返回false。
叶子节点 是指没有子节点的节点。
解法一: {{解法名称}}
解题思路
可以使用深度优先遍历的方式(本题前中后序都可以,无所谓,因为中节点也没有处理逻辑)来遍历二叉树
- 确定递归函数的参数和返回类型
参数:需要二叉树的根节点,还需要一个计数器,这个计数器用来计算二叉树的一条边之和是否正好是目标和,计数器为int型。
再来看返回值,递归函数什么时候需要返回值?什么时候不需要返回值?这里总结如下三点:
- 如果需要搜索整棵二叉树且不用处理递归返回值,递归函数就不要返回值。(这种情况就是本文下半部分介绍的113.路径总和ii)
- 如果需要搜索整棵二叉树且需要处理递归返回值,递归函数就需要返回值。 (这种情况我们在236. 二叉树的最近公共祖先 (opens new window)中介绍)
- 如果要搜索其中一条符合条件的路径,那么递归一定需要返回值,因为遇到符合条件的路径了就要及时返回。(本题的情况)
而本题我们要找一条符合条件的路径,所以递归函数需要返回值,及时返回,那么返回类型是什么呢?
如图所示:

图中可以看出,遍历的路线,并不要遍历整棵树,所以递归函数需要返回值,可以用bool类型表示。
所以代码如下:
boolean hasPathSum(TreeNode root, int targetSum) // 注意函数的返回类型
- 确定终止条件
首先计数器如何统计这一条路径的和呢?
不要去累加然后判断是否等于目标和,那么代码比较麻烦,可以用递减,让计数器count初始为目标和,然后每次减去遍历路径节点上的数值。
如果最后count == 0,同时到了叶子节点的话,说明找到了目标和。
如果遍历到了叶子节点,count不为0,就是没找到。
递归终止条件代码如下:
if (root.left == null && root.right == null){ // 遇到叶子节点,并且计数为0
if (targetSum == 0) return true;
if (targetSum != 0) return false;
if (root.left == null && root.right == null) return; // 遇到叶子节点而没有找到合适的边,直接返回
- 确定单层递归的逻辑
因为终止条件是判断叶子节点,所以递归的过程中就不要让空节点进入递归了。
递归函数是有返回值的,如果递归函数返回true,说明找到了合适的路径,应该立刻返回。
代码如下:
if (root.left != null){ // 左 (空节点不遍历)
// 遇到叶子节点返回true,则直接返回true
targetSum -= root.left.val;
if (hasPathSum(root.left,targetSum)) return true;
targetSum += root.left.val; // 注意这里有回溯的逻辑
}
if (root.right != null){ // 右 (空节点不遍历)
// 遇到叶子节点返回true,则直接返回true
targetSum -= root.right.val;
if (hasPathSum(root.right,targetSum)) return true;
targetSum += root.right.val; // 注意这里有回溯的逻辑
}
}
return false;
以上代码中是包含着回溯的
代码实现
public boolean hasPathSum(TreeNode root, int targetSum) {
if (root == null) return false;
if (root.left == null && root.right == null){
if (targetSum == 0) return true;
if (targetSum != 0) return false;
}
if (root.left != null){
targetSum -= root.left.val;
if (hasPathSum(root.left,targetSum)) return true;
targetSum += root.left.val;
}
if (root.right != null){
targetSum -= root.right.val;
if (hasPathSum(root.right,targetSum)) return true;
targetSum += root.right.val;
}
return false;
}
题目信息 106. 从中序与后序遍历序列构造二叉树
- 题目链接: https://leetcode.cn/problems/construct-binary-tree-from-inorder-and-postorder-traversal/
- 题目描述:
给定两个整数数组inorder和postorder,其中inorder是二叉树的中序遍历,postorder是同一棵树的后序遍历,请你构造并返回这颗 二叉树 。
解法一: {{解法名称}}
解题思路
首先回忆一下如何根据两个顺序构造一个唯一的二叉树,相信理论知识大家应该都清楚,就是以 后序数组的最后一个元素为切割点,先切中序数组,根据中序数组,反过来再切后序数组。一层一层切下去,每次后序数组最后一个元素就是节点元素。
如果让我们肉眼看两个序列,画一棵二叉树的话,应该分分钟都可以画出来。
流程如图:

那么代码应该怎么写呢?
说到一层一层切割,就应该想到了递归。
来看一下一共分几步:
-
第一步:如果数组大小为零的话,说明是空节点了。
-
第二步:如果不为空,那么取后序数组最后一个元素作为节点元素。
-
第三步:找到后序数组最后一个元素在中序数组的位置,作为切割点
-
第四步:切割中序数组,切成中序左数组和中序右数组 (顺序别搞反了,一定是先切中序数组)
-
第五步:切割后序数组,切成后序左数组和后序右数组
-
第六步:递归处理左区间和右区间
代码实现
private List<List<Integer>> result;
private List<Integer> path;
public List<List<Integer>> pathSum(TreeNode root, int sum) {
result = new ArrayList<>();
path = new ArrayList<>();
if (root == null) return result;
path.add(root.val); // 把根节点放进路径
traversal(root, sum - root.val);
return result;
}
// 递归函数不需要返回值,因为我们要遍历整个树
private void traversal(TreeNode root, int count) {
if (root.left == null && root.right == null && count == 0) {
// 遇到了叶子节点且找到了和为sum的路径
result.add(new ArrayList<>(path));
return; }
if (root.left == null && root.right == null) return; // 遇到叶子节点而没有找到合适的边,直接返回
if (root.left != null) { // 左 (空节点不遍历)
path.add(root.left.val);
count -= root.left.val;
traversal(root.left, count); // 递归
count += root.left.val; // 回溯
path.remove(path.size() - 1); // 回溯
}
if (root.right != null) { // 右 (空节点不遍历)
path.add(root.right.val);
count -= root.right.val;
traversal(root.right, count); // 递归
count += root.right.val; // 回溯
path.remove(path.size() - 1); // 回溯
}
}
代码实现
Map<Integer,Integer> map;
public TreeNode buildTree(int[] inorder, int[] postorder) {
map = new HashMap<>();
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 inBegin,int inEnd,int[] postorder,int postBegin,int postEnd){
if (inBegin >= inEnd || postBegin >= postEnd){
return null;
}
int rootIndex = map.get(postorder[postEnd - 1]);
TreeNode root = new TreeNode(inorder[rootIndex]);
int lenOfLeft = rootIndex - inBegin;
root.left = findNode(inorder,inBegin,rootIndex,postorder,postBegin,postBegin + lenOfLeft);
root.right = findNode(inorder,rootIndex + 1,inEnd,postorder,postBegin + lenOfLeft,postEnd - 1);
return root;
}
841

被折叠的 条评论
为什么被折叠?



