算法训练营(day18)

513. 找树左下角的值

题目链接:https://leetcode.cn/problems/find-bottom-left-tree-value/

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

解题思路

解题过程:

思路一:递归(回溯)

  • 最底层 的理解:位于二叉树的最大深度处

  • 定义回溯输出

    • 当遍历到叶子节点时,判断节点是否为 最底层 最左边 节点
    • 当出现 更底层 的节点,更新节点以及深度

思路二:层序遍历迭代(简单粗暴)

  • 最底层:层序遍历的最后一层
  • 最左边:最后一层的第一个节点

详细代码

解法一:递归

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
/**
* 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 {
int maxDep = -1;
int res = 0;
public int findBottomLeftValue(TreeNode root) {
res = root.val;
findLeftValue(root, 0);
return res;
}
public void findLeftValue(TreeNode node, int deep){
if(node == null){
return;
}
if(node.left == null && node.right == null){
if(deep > maxDep){
res = node.val;
maxDep = deep;
}
}

if(node.left != null){
findLeftValue(node.left, deep + 1);
}
if(node.right != null){
findLeftValue(node.right, deep + 1);
}
}
}

解法二:层序遍历迭代

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
/**
* 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 int findBottomLeftValue(TreeNode root) {
Queue<TreeNode> queue = new LinkedList<>();
queue.offer(root);
int res = 0;

while(!queue.isEmpty()){
int size = queue.size();

for(int i = 0; i < size; i++){
TreeNode tmpNode = queue.poll();
if(i == 0){
res = tmpNode.val;
}
if(tmpNode.left != null){
queue.offer(tmpNode.left);
}
if(tmpNode.right != null){
queue.offer(tmpNode.right);
}
}
}
return res;
}
}

112. 路径总和

题目链接:https://leetcode.cn/problems/path-sum/description/

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

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

解题思路

思路一:递归

  • targetSum 不断减去节点值,一直到叶子节点

  • 如果最后 targetSum 能等于0,则说明存在路径

思路二:迭代

  • 定义两个栈,用于存储 节点 以及 节点值

  • 依次弹出节点以及节点值,定义一个累加值 sum

    • 如果到叶子节点的 sum 等于 targetSum,则说明存在路径,返回true
  • 如果没到叶子节点,则两个栈存储 下一个节点 以及 sum + 下一个节点值

  • 遍历整个二叉树,如果没有路径,则返回 false

详细代码

解法一:递归

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
/**
* 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;
}
if(root.left == null && root.right == null){
return root.val == targetSum;
}
return hasPathSum(root.left, targetSum - root.val) || hasPathSum(root.right, targetSum - root.val);
}
}

解法二:迭代

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
/**
* 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;
}
Stack<TreeNode> stackRoot = new Stack<>();
Stack<Integer> stackRootValue = new Stack<>();

stackRoot.push(root);
stackRootValue.push(root.val);

while(!stackRoot.isEmpty()){
int size = stackRoot.size();

while(size-- > 0){
TreeNode tmpNode = stackRoot.pop();
int sum = stackRootValue.pop();

if(tmpNode.left == null && tmpNode.right == null && sum == targetSum){
return true;
}
if(tmpNode.left != null){
stackRoot.push(tmpNode.left);
stackRootValue.push(sum + tmpNode.left.val);
}
if(tmpNode.right != null){
stackRoot.push(tmpNode.right);
stackRootValue.push(sum + tmpNode.right.val);
}
}
}
return false;
}
}

113. 路径总和 II

题目链接:https://leetcode.cn/problems/path-sum-ii/description/

给你二叉树的根节点 root 和一个整数目标和 targetSum ,找出所有 从根节点到叶子节点 路径总和等于给定目标和的路径。

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

解题思路

解题过程:递归(回溯)

  • 定义一个集合 path 存储每次遍历的节点值
  • targetSum 不断减去节点值,一直到叶子节点
  • 如果最后 targetSum 能等于叶子节点的值,则说明存在路径
  • 用集合 res 存储每个满足题意的 path
  • 使用 回溯,对二叉树进行遍历 path.remove(path.size() - 1)

详细代码

解法一:递归

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
/**
* 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 {
List<List<Integer>> res = new ArrayList<>();
List<Integer> path = new ArrayList<>();

public List<List<Integer>> pathSum(TreeNode root, int targetSum) {
findPath(root, targetSum);
return res;
}

public void findPath(TreeNode node, int targetSum){
if(node == null){
return;
}
path.add(node.val);

if(node.left == null && node.right == null){
if(node.val == targetSum){
res.add(new ArrayList<>(path));
}
return;
}
if(node.left != null){
findPath(node.left, targetSum - node.val);
path.remove(path.size() - 1);
}
if(node.right != null){
findPath(node.right, targetSum - node.val);
path.remove(path.size() - 1);
}
}
}

解法二:递归

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
/**
* 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 {
List<List<Integer>> res = new LinkedList<>();
List<Integer> path = new LinkedList<>();

public List<List<Integer>> pathSum(TreeNode root, int targetSum) {
findPath(root, targetSum);
return res;
}

public void findPath(TreeNode node, int targetSum){
if(node == null){
return;
}
path.add(node.val);
targetSum -= node.val;

if(node.left == null && node.right == null && targetSum == 0){
res.add(new LinkedList<>(path));
}

findPath(node.left, targetSum);
findPath(node.right, targetSum);
path.remove(path.size() - 1);
}
}

解法三:递归

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
/**
* 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 {
List<List<Integer>> res = new LinkedList<>();
LinkedList<Integer> path = new LinkedList<>();

public List<List<Integer>> pathSum(TreeNode root, int targetSum) {
findPath(root, targetSum);
return res;
}

public void findPath(TreeNode node, int targetSum){
if(node == null){
return;
}
path.add(node.val);
targetSum -= node.val;

if(node.left == null && node.right == null && targetSum == 0){
res.add(new LinkedList<>(path));
}

findPath(node.left, targetSum);
findPath(node.right, targetSum);
path.removeLast();
}
}

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

题目链接:https://leetcode.cn/problems/construct-binary-tree-from-inorder-and-postorder-traversal/description/

给定两个整数数组 inorderpostorder ,其中 inorder 是二叉树的中序遍历, postorder 是同一棵树的后序遍历,请你构造并返回这颗 二叉树

解题思路

解题过程:关键点在于左右子树边界的界定(这里使用的是 左闭右开 )

  1. 定义一个 map,用于存储中序遍历的下标
  2. 后序遍历 中找到二叉树的 根节点
  3. 再根据根节点在 中序遍历 的位置,从 后序遍历 中获取左右子树
  4. 递归这个过程,得到二叉树

详细代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
/**
* 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 {
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 rebuild(inorder, 0, inorder.length, postorder, 0, postorder.length);
}

public TreeNode rebuild(int[] inorder, int inStart, int inEnd, int[] postorder, int postStart, int postEnd){
if(inStart >= inEnd || postStart >= postEnd){
return null;
}
int rootIndex = map.get(postorder[postEnd - 1]);
TreeNode root = new TreeNode(inorder[rootIndex]); //找到根节点

int postorderLeftLen = rootIndex - inStart;
root.left = rebuild(inorder, inStart, rootIndex, postorder, postStart, postStart + postorderLeftLen);
root.right = rebuild(inorder, rootIndex + 1, inEnd, postorder, postStart + postorderLeftLen, postEnd - 1);
return root;
}
}

105. 从前序与中序遍历序列构造二叉树

题目链接:https://leetcode.cn/problems/construct-binary-tree-from-preorder-and-inorder-traversal/description/

给定两个整数数组 preorderinorder ,其中 preorder 是二叉树的先序遍历inorder 是同一棵树的中序遍历,请构造二叉树并返回其根节点。

解题思路

解题过程:关键点在于左右子树边界的界定(这里使用的是 左闭右开 )

  1. 定义一个 map,用于存储中序遍历的下标
  2. 前序遍历 中找到二叉树的 根节点
  3. 再根据根节点在 中序遍历 的位置,从 前序遍历 中获取左右子树
  4. 递归这个过程,得到二叉树

详细代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
/**
* 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 {
Map<Integer, Integer> map;
public TreeNode buildTree(int[] preorder, int[] inorder) {
map = new HashMap<>();
for(int i = 0; i < inorder.length; i++){
map.put(inorder[i], i);
}
return rebuild(preorder, 0, preorder.length, inorder, 0, inorder.length);
}

public TreeNode rebuild(int[] preorder, int preStart, int preEnd, int[] inorder, int inStart, int inEnd){
if(preStart >= preEnd || inStart >= inEnd){
return null;
}
int rootIndex = map.get(preorder[preStart]);
TreeNode root = new TreeNode(inorder[rootIndex]);

int preorderLeftLen = rootIndex - inStart;
root.left = rebuild(preorder, preStart + 1, preStart + 1 + preorderLeftLen, inorder, inStart, rootIndex);
root.right = rebuild(preorder, preStart + 1 + preorderLeftLen, preEnd, inorder, rootIndex + 1, inEnd);
return root;
}
}