代码随想录算法训练营第18天 | 513.找树左下角的值、112. 路径总和、106.从中序与后序遍历序列构造二叉树
513.找树左下角的值
题目:513.找树左下角的值
文档讲解:代码随想录-513.找树左下角的值
视频讲解:哔哩哔哩-513.找树左下角的值
状态/时间:没写出来/三十分钟
思路:
我们来分析一下题目:在树的最后一行找到最左边的值。
首先要是最后一行,然后是最左边的值。
如果使用递归法,如何判断是最后一行呢,其实就是深度最大的叶子节点一定是最后一行。
代码:
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
private int Deep = -1;
private int value = 0;
public int findBottomLeftValue(TreeNode root) {
if (root == null) {
return 0;
}
value = root.val;
findLeftValue(root, 0);
return value;
}
public void findLeftValue(TreeNode root, int deep) {
if (root.left == null && root.right == null) {
if (deep > Deep) {
value = root.val;
Deep = deep;
}
}
// 这里用到了回溯
if (root.left != null) {
deep++;
findLeftValue(root.left, deep);
deep--;
}
if (root.right != null) {
deep++;
findLeftValue(root.right, deep);
deep--;
}
}
}
注意:
112. 路径总和
题目:112. 路径总和
文档讲解:代码随想录-112. 路径总和
视频讲解:哔哩哔哩-112. 路径总和
状态/时间:没写出来/三十分钟
思路:
可以使用深度优先遍历的方式(本题前中后序都可以,无所谓,因为中节点也没有处理逻辑)来遍历二叉树。
代码:
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
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;
}
}
注意:
106.从中序与后序遍历序列构造二叉树
题目:106.从中序与后序遍历序列构造二叉树
文档讲解:代码随想录-106.从中序与后序遍历序列构造二叉树
视频讲解:哔哩哔哩-106.从中序与后序遍历序列构造二叉树
状态/时间:没写出来/三十分钟
思路:
- 第一步:如果数组大小为零的话,说明是空节点了。
- 第二步:如果不为空,那么取后序数组最后一个元素作为节点元素。
- 第三步:找到后序数组最后一个元素在中序数组的位置,作为切割点
- 第四步:切割中序数组,切成中序左数组和中序右数组 (顺序别搞反了,一定是先切中序数组)
- 第五步:切割后序数组,切成后序左数组和后序右数组
- 第六步:递归处理左区间和右区间
代码:
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
// inorder 前序 postorder 后序
public TreeNode buildTree(int[] inorder, int[] postorder) {
if (inorder == null || postorder == null) {
return null;
}
return buildHelper(inorder, 0, inorder.length, postorder, 0, postorder.length);
}
private TreeNode buildHelper(int[] inorder, int inorderStart, int inorderEnd, int[] postorder, int postorderStart, int postorderEnd) {
// 第一步
if(postorderStart == postorderEnd)
return null;
// 后序遍历的最后一个元素,其实就是根节点
int rootVal = postorder[postorderEnd - 1];
TreeNode root = new TreeNode(rootVal);
// 找切割点
int middleIndex = 0;
for (middleIndex = 0; middleIndex < postorder.length; middleIndex++) {
if (inorder[middleIndex] == rootVal) {
break;
}
}
// 第四步:切割中序数组,得到 中序左数组和中序右数组
int leftInorderStart = inorderStart;
int leftInorderEnd = middleIndex;
int leftPostorderStart = postorderStart;
int leftPostorderEnd = postorderStart + (middleIndex - inorderStart);
// 第五步:切割后序数组,得到 后序左数组和后序右数组
int rightInorderStart = middleIndex + 1;
int rightInorderEnd = inorderEnd;
int rightPostorderStart = leftPostorderEnd;
int rightPostorderEnd = postorderEnd - 1;
// 第六步
root.left = buildHelper(inorder, leftInorderStart, leftInorderEnd, postorder, leftPostorderStart, leftPostorderEnd);
root.right = buildHelper(inorder, rightInorderStart, rightInorderEnd, postorder, rightPostorderStart, rightPostorderEnd);
return root;
}
}
注意: