# LeetCode深度优先算法之树(路径相关)

## LeetCode深度优先算法之树(路径相关)

### 112.路径总和

#### 描述

``````          5
/ \
4   8
/   / \
11  13  4
/  \      \
7    2      1``````

#### 思路

``````if (node.left == null && node.right == null)
``````

sum的和与其判断的是节点的总和，所以这是一个累加的和。我们需要记录每个节点的和，先记录后遍历子节点，这种场景就是dfs典型的场景。

``````addVal();
recursion();``````

dfs可以使用递归进行实现，目前已经确定了可以使用dfs进行解决，递归的结束的条件是节点为null或者当前节点的左右节点为null。还有一个问题，我们如何确定当前叶子节点累加的值等于目标值呢？

``````if sum == node1.val + node2.val + node3.val;
then
node3.val = sum - node2.val - node1.val;
``````

#### 代码

``````class Solution {
public boolean hasPathSum(TreeNode root, int sum) {
if (root == null) return false;
if (root.left == null && root.right == null) return sum == root.val;

return hasPathSum(root.left, sum - root.val) ||
hasPathSum(root.right, sum - root.val);
}
}
``````

### 113.路径总和II

#### 题目描述

``````          5
/ \
4   8
/   / \
11  13  4
/  \    / \
7    2  5   1``````

[
[5,4,11,2],
[5,8,4,5]
]

#### 代码

``````class Solution {

List<List<Integer>> res = new ArrayList<>();
public List<List<Integer>> pathSum(TreeNode root, int sum) {
if (root == null) return res;

List<Integer> list = new ArrayList<>();
this.backTrack(list, root, sum);

return res;
}

void backTrack(List<Integer> list, TreeNode node, int sum) {
if (node == null) return ;

if (node.left == null && node.right == null) {
if (node.val == sum) {
}

list.remove(list.size() - 1);
return;
}

backTrack(list, node.left, sum - node.val);
backTrack(list, node.right, sum - node.val);

list.remove(list.size() - 1);
}
}``````

### 129.求根到叶子节点数字之和

#### 题目描述

``````输入: [1,2,3]
1
/ \
2   3

4
/ \
9   0
/ \
5   1

``````

#### 思路

``````recordValue();
recursion(left);
recursion(right);``````

#### 代码

``````class Solution {
public int sumNumbers(TreeNode root) {
if (root == null) return 0;

return dfs(root, 0);
}

int dfs(TreeNode root, int preNum) {
if (root == null) return 0;

int num = preNum * 10 + root.val;
if (root.left == null && root.right == null) return num;

int leftSum = dfs(root.left, num);
int rightSum = dfs(root.right, num);

return leftSum + rightSum;
}
}

``````

### 257.二叉树的所有路径

#### 题目描述

``````

1
/   \
2     3
\
5

``````

#### 代码

``````class Solution {

List<String> res = new ArrayList<>();
public List<String> binaryTreePaths(TreeNode root) {
if (root == null) return res;

this.backTrack(new StringBuilder(""), root);
return res;
}

public void backTrack(StringBuilder sb, TreeNode node) {
if (node == null) return;

if (node.left == null && node.right == null) {
return;
}

int delIndex = sb.length();
sb.append(node.val).append("->");
backTrack(sb, node.left);
backTrack(sb, node.right);

sb.delete(delIndex, sb.length());
}
}``````

### 程序员面试金典04.12 求和路径

#### 题目描述

``````          5
/ \
4   8
/   / \
11  13  4
/  \    / \
7    2  5   1``````

3

#### 代码

``````class Solution {

int res = 0;
public int pathSum(TreeNode root, int sum) {
if (root == null) return res;

int depth = depth(root);
int[] levels = new int[depth];
this.bfs(root, levels, 0, sum);

return res;
}

void bfs(TreeNode root, int[] levels, int level, int num) {
if (root == null) return;

levels[level] = root.val;
int sum = 0;
for (int i = level; i >= 0; i--) {
sum += levels[i];
if (sum == num) {
res += 1;
}
}

bfs(root.left, levels, level + 1, num);
bfs(root.right, levels, level + 1, num);
}

int depth(TreeNode root) {
if (root == null) return 0;

int left = depth(root.left);
int right = depth(root.right);

return left > right ? left + 1 : right + 1;
}
}``````
0 声望
0 粉丝
0 条评论