112 路径总和

  1. /**
  2. * Definition for a binary tree node.
  3. * public class TreeNode {
  4. * int val;
  5. * TreeNode left;
  6. * TreeNode right;
  7. * TreeNode(int x) { val = x; }
  8. * }
  9. */
  10. class Solution {
  11. public boolean hasPathSum(TreeNode root, int sum) {
  12. if (root == null)
  13. return false;
  14. if (root.left == null && root.right == null )
  15. return (root.val == sum);
  16. return hasPathSum(root.left, sum - root.val) || hasPathSum(root.right, sum - root.val);
  17. }
  18. }