112. Path Sum

只有在两个子节点都是null的叶节点才有可能符合条件\

 

 

 1 class Solution {
 2     public boolean hasPathSum(TreeNode root, int sum) {
 3         if(root == null) return false;
 4         return dfs(root, sum);
 5         
 6     }
 7     
 8     public boolean dfs(TreeNode root, int remain) {
 9         if(root == null) return false;
10         remain -= root.val;
11         if(root.left == null && root.right == null && remain == 0) {
12             return true;
13         }    
14         return(dfs(root.right, remain) || dfs(root.left, remain));
15         
16         
17     }
18 }

 

posted @ 2018-09-21 08:46  jasoncool1  阅读(153)  评论(0编辑  收藏  举报