Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit aec31a7

Browse filesBrowse files
committed
Tree - hasPathSum
1 parent 451dd70 commit aec31a7
Copy full SHA for aec31a7

File tree

1 file changed

+12
-0
lines changed
Filter options

1 file changed

+12
-0
lines changed

‎src/leetcode2018/Trees.java

Copy file name to clipboardExpand all lines: src/leetcode2018/Trees.java
+12Lines 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 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.