Skip to content

Commit a4faad7

Browse files
committed
feat: add 112
1 parent 6c71c2a commit a4faad7

File tree

5 files changed

+144
-41
lines changed

5 files changed

+144
-41
lines changed

README.md

+2
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
|108|[Convert Sorted Array to Binary Search Tree][108]|Tree, Depth-first Search|
3636
|110|[Balanced Binary Tree][110]|Tree, Depth-first Search|
3737
|111|[Minimum Depth of Binary Tree][111]|Tree, Depth-first Search, Breadth-first Search|
38+
|112|[Path Sum][112]|Tree, Depth-first Search|
3839

3940

4041
## Medium
@@ -85,6 +86,7 @@
8586
[108]: https://github.com/Blankj/awesome-java-leetcode/blob/master/note/108/README.md
8687
[110]: https://github.com/Blankj/awesome-java-leetcode/blob/master/note/110/README.md
8788
[111]: https://github.com/Blankj/awesome-java-leetcode/blob/master/note/111/README.md
89+
[112]: https://github.com/Blankj/awesome-java-leetcode/blob/master/note/112/README.md
8890

8991
[008]: https://github.com/Blankj/awesome-java-leetcode/blob/master/note/008/README.md
9092
[019]: https://github.com/Blankj/awesome-java-leetcode/blob/master/note/019/README.md

note/112/README.md

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
# [Path Sum][title]
2+
3+
## Description
4+
5+
Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all the values along the path equals the given sum.
6+
7+
For example:
8+
9+
Given the below binary tree and `sum = 22`,
10+
11+
```
12+
5
13+
/ \
14+
4 8
15+
/ / \
16+
11 13 4
17+
/ \ \
18+
7 2 1
19+
20+
```
21+
22+
return true, as there exist a root-to-leaf path `5->4->11->2` which sum is 22.
23+
24+
**Tags:** Tree, Depth-first Search
25+
26+
27+
## 思路
28+
29+
题意是查找二叉树中是否存在从根结点到叶子的路径和为某一值,利用深搜在遇到叶子节点时判断是否满足即可。
30+
31+
32+
``` java
33+
/**
34+
* Definition for a binary tree node.
35+
* public class TreeNode {
36+
* int val;
37+
* TreeNode left;
38+
* TreeNode right;
39+
* TreeNode(int x) { val = x; }
40+
* }
41+
*/
42+
class Solution {
43+
public boolean hasPathSum(TreeNode root, int sum) {
44+
if (root == null) return false;
45+
if (root.left == null && root.right == null) return sum == root.val;
46+
return hasPathSum(root.left, sum - root.val) || hasPathSum(root.right, sum - root.val);
47+
}
48+
}
49+
```
50+
51+
52+
## 结语
53+
54+
如果你同我一样热爱数据结构、算法、LeetCode,可以关注我GitHub上的LeetCode题解:[awesome-java-leetcode][ajl]
55+
56+
57+
58+
[title]: https://leetcode.com/problems/path-sum
59+
[ajl]: https://github.com/Blankj/awesome-java-leetcode

project/leetcode/.idea/workspace.xml

+56-41
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Binary file not shown.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.blankj.easy._112;
2+
3+
4+
import com.blankj.structure.TreeNode;
5+
6+
/**
7+
* <pre>
8+
* author: Blankj
9+
* blog : http://blankj.com
10+
* time : 2017/10/09
11+
* desc :
12+
* </pre>
13+
*/
14+
public class Solution {
15+
public boolean hasPathSum(TreeNode root, int sum) {
16+
if (root == null) return false;
17+
if (root.left == null && root.right == null) return sum == root.val;
18+
return hasPathSum(root.left, sum - root.val) || hasPathSum(root.right, sum - root.val);
19+
}
20+
21+
public static void main(String[] args) {
22+
Solution solution = new Solution();
23+
TreeNode testData = TreeNode.createTestData("[5,4,8,11,null,13,4,7,2,null,null,null,1]");
24+
TreeNode.print(testData);
25+
System.out.println(solution.hasPathSum(testData, 22));
26+
}
27+
}

0 commit comments

Comments
 (0)