112. Path Sum

没啥要说的

 1     public boolean hasPathSum(TreeNode root, int sum) {
 2         if(root == null) {
 3             return false;
 4         }
 5         return helper(root, sum, 0);
 6     }
 7     
 8     private boolean helper(TreeNode root, int sum, int curSum) {
 9         if(root == null) {
10             return false;
11         }
12         if(root.left == null && root.right == null) {
13             return (curSum + root.val) == sum;
14         }
15         return helper(root.left, sum, curSum + root.val) || helper(root.right, sum, curSum + root.val);
16     }

 

posted @ 2016-06-12 04:46  warmland  阅读(132)  评论(0编辑  收藏  举报