• 欢迎光临~

# 代码随想录训练营｜Day 18｜513, 112, 113, 105, 106

### 513. Find Bottom Left Tree Value

Given the `root` of a binary tree, return the leftmost value in the last row of the tree.

Example 1:

``````Input: root = [2,1,3]
Output: 1
``````

Example 2:

``````Input: root = [1,2,3,4,null,5,6,null,null,7]
Output: 7
``````

Constraints:

• The number of nodes in the tree is in the range `[1, 104]`.
• `231 <= Node.val <= 231 - 1`

``````class Solution {
private int Deep = -1;
private int value = 0;
public int findBottomLeftValue(TreeNode root) {
value = root.val;
findLeftValue(root,0);
return value;
}

private void findLeftValue (TreeNode root,int deep) {
if (root == null) return;
if (root.left == null && root.right == null) {
if (deep > Deep) {
value = root.val;
Deep = deep;
}
}
if (root.left != null) findLeftValue(root.left,deep + 1);
if (root.right != null) findLeftValue(root.right,deep + 1);
}
}
``````

For Future References

### 112. Path Sum

Given the `root` of a binary tree and an integer `targetSum`, return `true` if the tree has a root-to-leaf path such that adding up all the values along the path equals `targetSum`.

leaf is a node with no children.

Example 1:

``````Input: root = [5,4,8,11,null,13,4,7,2,null,null,null,1], targetSum = 22
Output: true
Explanation: The root-to-leaf path with the target sum is shown.
``````

Example 2:

``````Input: root = [1,2,3], targetSum = 5
Output: false
Explanation: There two root-to-leaf paths in the tree:
(1 --> 2): The sum is 3.
(1 --> 3): The sum is 4.
There is no root-to-leaf path with sum = 5.
``````

Example 3:

``````Input: root = [], targetSum = 0
Output: false
Explanation: Since the tree is empty, there are no root-to-leaf paths.
``````

Constraints:

• The number of nodes in the tree is in the range `[0, 5000]`.
• `1000 <= Node.val <= 1000`
• `1000 <= targetSum <= 1000`

• 如果需要搜索整棵二叉树且不用处理递归返回值，递归函数就不要返回值。
• 如果需要搜索整棵二叉树且需要处理递归返回值，递归函数就需要返回值。
• 如果要搜索其中一条符合条件的路径，那么递归一定需要返回值，因为遇到符合条件的路径了就要及时返回。（本题的情况）

``````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;
}
}
``````

For Future References

### 113. Path Sum II

Given the `root` of a binary tree and an integer `targetSum`, return all root-to-leaf paths where the sum of the node values in the path equals `targetSum`. Each path should be returned as a list of the node values, not node references.

root-to-leaf path is a path starting from the root and ending at any leaf node. A leaf is a node with no children.

Example 1:

``````Input: root = [5,4,8,11,null,13,4,7,2,null,null,5,1], targetSum = 22
Output: [[5,4,11,2],[5,8,4,5]]
Explanation: There are two paths whose sum equals targetSum:
5 + 4 + 11 + 2 = 22
5 + 8 + 4 + 5 = 22
``````

Example 2:

``````Input: root = [1,2,3], targetSum = 5
Output: []
``````

Example 3:

``````Input: root = [1,2], targetSum = 0
Output: []
``````

Constraints:

• The number of nodes in the tree is in the range `[0, 5000]`.
• `1000 <= Node.val <= 1000`
• `1000 <= targetSum <= 1000`

``````class solution {
public List<List<Integer>> pathsum(TreeNode root, int targetsum) {
List<List<Integer>> res = new ArrayList<>();
if (root == null) return res; // 非空判断

preorderdfs(root, targetsum, res, path);
return res;
}

public void preorderdfs(TreeNode root, int targetsum, List<List<Integer>> res, List<Integer> path) {
// 遇到了叶子节点
if (root.left == null && root.right == null) {
// 找到了和为 targetsum 的路径
if (targetsum - root.val == 0) {
}
return; // 如果和不为 targetsum，返回
}

if (root.left != null) {
preorderdfs(root.left, targetsum - root.val, res, path);
path.remove(path.size() - 1); // 回溯
}
if (root.right != null) {
preorderdfs(root.right, targetsum - root.val, res, path);
path.remove(path.size() - 1); // 回溯
}
}
}
``````

For Future References

### 106. Construct Binary Tree from Inorder and Postorder Traversal

Given two integer arrays `inorder` and `postorder` where `inorder` is the inorder traversal of a binary tree and `postorder` is the postorder traversal of the same tree, construct and return the binary tree.

Example 1:

``````Input: inorder = [9,3,15,20,7], postorder = [9,15,7,20,3]
Output: [3,9,20,null,null,15,7]
``````

Example 2:

``````Input: inorder = [-1], postorder = [-1]
Output: [-1]
``````

Constraints:

• `1 <= inorder.length <= 3000`
• `postorder.length == inorder.length`
• `3000 <= inorder[i], postorder[i] <= 3000`
• `inorder` and `postorder` consist of unique values.
• Each value of `postorder` also appears in `inorder`.
• `inorder` is guaranteed to be the inorder traversal of the tree.
• `postorder` is guaranteed to be the postorder traversal of the tree.

``````class Solution {
Map<Integer, Integer> map;  // 方便根据数值查找位置
public TreeNode buildTree(int[] inorder, int[] postorder) {
map = new HashMap<>();
for (int i = 0; i < inorder.length; i++) { // 用map保存中序序列的数值对应位置
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;
}
}
``````

For Future References

### 105. Construct Binary Tree from Preorder and Inorder Traversal

Given two integer arrays `preorder` and `inorder` where `preorder` is the preorder traversal of a binary tree and `inorder` is the inorder traversal of the same tree, construct and return the binary tree.

Example 1:

``````Input: preorder = [3,9,20,15,7], inorder = [9,3,15,20,7]
Output: [3,9,20,null,null,15,7]
``````

Example 2:

``````Input: preorder = [-1], inorder = [-1]
Output: [-1]
``````

Constraints:

• `1 <= preorder.length <= 3000`
• `inorder.length == preorder.length`
• `3000 <= preorder[i], inorder[i] <= 3000`
• `preorder` and `inorder` consist of unique values.
• Each value of `inorder` also appears in `preorder`.
• `preorder` is guaranteed to be the preorder traversal of the tree.
• `inorder` is guaranteed to be the inorder traversal of the tree.
``````class Solution {
Map<Integer, Integer> map;
public TreeNode buildTree(int[] preorder, int[] inorder) {
map = new HashMap<>();
for (int i = 0; i < inorder.length; i++) { // 用map保存中序序列的数值对应位置
map.put(inorder[i], i);
}

return findNode(preorder, 0, preorder.length, inorder,  0, inorder.length);  // 前闭后开
}

public TreeNode findNode(int[] preorder, int preBegin, int preEnd, int[] inorder, int inBegin, int inEnd) {
// 参数里的范围都是前闭后开
if (preBegin >= preEnd || inBegin >= inEnd) {  // 不满足左闭右开，说明没有元素，返回空树
return null;
}
int rootIndex = map.get(preorder[preBegin]);  // 找到前序遍历的第一个元素在中序遍历中的位置
TreeNode root = new TreeNode(inorder[rootIndex]);  // 构造结点
int lenOfLeft = rootIndex - inBegin;  // 保存中序左子树个数，用来确定前序数列的个数
root.left = findNode(preorder, preBegin + 1, preBegin + lenOfLeft + 1,
inorder, inBegin, rootIndex);
root.right = findNode(preorder, preBegin + lenOfLeft + 1, preEnd,
inorder, rootIndex + 1, inEnd);

return root;
}
}
``````

For Future References