二叉树中和为某一值的路径(一)
运用深搜的思想,进行最后边界条件判断即可。
public boolean hasPathSum (TreeNode root, int sum) {
// write code here
if(root==null) return false;
sum-=root.val;
if(sum==0&&root.left==null&&root.right==null) return true;
return hasPathSum(root.left,sum)||hasPathSum(root.right,sum);
}