forked from DreamCats/java-notes
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathT7.java
More file actions
35 lines (33 loc) · 832 Bytes
/
Copy pathT7.java
File metadata and controls
35 lines (33 loc) · 832 Bytes
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
/**
* @program JavaBooks
* @description: 路径总和
* @author: mf
* @create: 2020/03/10 14:14
*/
package subject.tree;
/**
* 5
* / \
* 4 8
* / / \
* 11 13 4
* / \ \
* 7 2 1
* sum = 22
*/
public class T7 {
/**
* 递归
* @param root
* @param sum
* @return
*/
public boolean hasPathSum(TreeNode root, int sum) {
if (root == null) return false;
// 总和减去路径上节点的值
sum -= root.val;
// 如果节的左右节点都为空,说明那个路径上走完了,可以开始比较sum是否为0
if (root.left == null && root.right == null) return sum == 0;
return hasPathSum(root.left, sum) || hasPathSum(root.right, sum);
}
}