Skip to content

Commit 7566e15

Browse files
path sum
1 parent 4e9a62b commit 7566e15

File tree

2 files changed

+25
-12
lines changed

2 files changed

+25
-12
lines changed

‎EASY/src/easy/PathSum.java

+24-12
Original file line numberDiff line numberDiff line change
@@ -27,25 +27,37 @@ public boolean hasPathSum(TreeNode root, int sum) {
2727

2828
public static void main(String...strings){
2929
PathSum test = new PathSum();
30-
// TreeNode root = new TreeNode(1);
31-
// root.left = new TreeNode(2);
32-
// int sum = 1;
30+
TreeNode root = new TreeNode(5);
31+
root.left = new TreeNode(4);
32+
int sum = 5;
3333

34-
TreeNode root = new TreeNode(1);
35-
root.left = new TreeNode(-2);
36-
root.left.left = new TreeNode(1);
37-
root.left.right = new TreeNode(3);
38-
root.right = new TreeNode(-3);
39-
root.right.left = new TreeNode(-2);
40-
root.left.left.left = new TreeNode(-1);
41-
int sum = 2;
34+
// TreeNode root = new TreeNode(1);
35+
// root.left = new TreeNode(-2);
36+
// root.left.left = new TreeNode(1);
37+
// root.left.right = new TreeNode(3);
38+
// root.right = new TreeNode(-3);
39+
// root.right.left = new TreeNode(-2);
40+
// root.left.left.left = new TreeNode(-1);
41+
// int sum = 2;
4242
// 1
4343
// / \
4444
// -2 -3
4545
// / \ /
4646
// 1 3 -2
4747
// /
4848
// -1
49-
System.out.println(test.hasPathSum(root, sum));
49+
// System.out.println(test.hasPathSum(root, sum));
50+
System.out.println(test.hasPathSumAgain(root, sum));
5051
}
52+
53+
54+
public boolean hasPathSumAgain(TreeNode root, int sum) {
55+
if(root == null) return false;
56+
if(root.left == null && root.right == null){
57+
if(sum == root.val) return true;
58+
else return false;
59+
}
60+
return hasPathSum(root.left, sum - root.val) || hasPathSum(root.right, sum - root.val);
61+
}
62+
5163
}

‎README.md

+1
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
|118|[Pascal's Triangle](https://leetcode.com/problems/pascals-triangle/)|[Solution](../../blob/master/EASY/src/easy/PascalsTriangle.java)| O(n^2)|O(1) | Easy|
4242
|117|[Populating Next Right Pointers in Each Node II](https://leetcode.com/problems/populating-next-right-pointers-in-each-node-ii/)|[Solution](../../blob/master/HARD/src/hard/PopulatingNextRightPointersinEachNodeII.java)| O(n)|O(1) | Hard| BFS
4343
|116|[Populating Next Right Pointers in Each Node](https://leetcode.com/problems/populating-next-right-pointers-in-each-node/)|[Solution](../../blob/master/MEDIUM/src/medium/PopulatingNextRightPointersinEachNode.java)| O(n)|O(1) | Medium| BFS
44+
|112|[Path Sum](https://leetcode.com/problems/path-sum/)|[Solution](../../blob/master/EASY/src/easy/PathSum.java)| O(n)|O(1) | Easy| DFS
4445
|111|[Minimum Depth of Binary Tree](https://leetcode.com/problems/minimum-depth-of-binary-tree/)|[Solution](../../blob/master/EASY/src/easy/MinimumDepthofBinaryTree.java)| O(n)|O(1)~O(h) | Easy| BFS, DFS
4546
|91|[Decode Ways](https://leetcode.com/problems/decode-ways/)|[Solution](../../blob/master/MEDIUM/src/medium/DecodeWays.java)| O(n)|O(n) | Medium| DP
4647
|98|[Validate Binary Search Tree](https://leetcode.com/problems/validate-binary-search-tree/)|[Solution] | O(n) | O(1) | Medium | DFS/Recursion

0 commit comments

Comments
 (0)