Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit aec31a7

Browse files
committed
Tree - hasPathSum
1 parent 451dd70 commit aec31a7

File tree

1 file changed

+12
-0
lines changed

1 file changed

+12
-0
lines changed

src/leetcode2018/Trees.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
import java.util.List;
66
import java.util.Queue;
77

8+
import amazon.TreeNode;
9+
810

911
public class Trees {
1012

@@ -134,6 +136,16 @@ public List<List<Integer>> levelOrder(TreeNode root) {
134136
return re;
135137
}
136138

139+
public boolean hasPathSum(TreeNode root, int sum) {
140+
if(root==null) return false;
141+
System.out.println(root.val);
142+
if(root.left==null && root.right==null) {
143+
System.out.println(root.val +" ="+ sum);
144+
return sum == root.val;
145+
}
146+
return hasPathSum(root.left, sum-root.val) || hasPathSum(root.right,sum-root.val);
147+
}
148+
137149
}
138150

139151
class TreeNode{

0 commit comments

Comments
 (0)