File tree Expand file tree Collapse file tree 1 file changed +33
-0
lines changed Expand file tree Collapse file tree 1 file changed +33
-0
lines changed Original file line number Diff line number Diff line change 1+ /**
2+ * Definition for a binary tree node.
3+ * public class TreeNode {
4+ * int val;
5+ * TreeNode left;
6+ * TreeNode right;
7+ * TreeNode() {}
8+ * TreeNode(int val) { this.val = val; }
9+ * TreeNode(int val, TreeNode left, TreeNode right) {
10+ * this.val = val;
11+ * this.left = left;
12+ * this.right = right;
13+ * }
14+ * }
15+ */
16+ class Solution {
17+ private boolean isLeafNode (TreeNode node ) {
18+ return ((node .left == null ) && (node .right == null ));
19+ }
20+
21+ public boolean hasPathSum (TreeNode root , int targetSum ) {
22+ // Edge case: No nodes
23+ if (root == null ) {
24+ return false ;
25+ }
26+
27+ targetSum -= root .val ;
28+ if (isLeafNode (root )) {
29+ return (targetSum == 0 );
30+ }
31+ return hasPathSum (root .left , targetSum ) || hasPathSum (root .right , targetSum );
32+ }
33+ }
You can’t perform that action at this time.
0 commit comments