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 }