112 Path Sum 路径总和

给定一棵二叉树和一个总和,确定该树中是否存在根到叶的路径,这条路径的所有值相加等于给定的总和。
例如:
给定下面的二叉树和 总和 = 22,
              5
             / \
            4   8
           /   / \
          11  13  4
         /  \      \
        7    2      1
返回 true, 因为存在总和为 22 的根到叶的路径 5->4->11->2。
详见:https://leetcode.com/problems/path-sum/description/

Java实现:

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
class Solution {
    public boolean hasPathSum(TreeNode root, int sum) {
        if(root==null){
            return false;
        }else if(root.left==null&&root.right==null&&root.val==sum){
            return true;
        }
        return (hasPathSum(root.left,sum-root.val)||hasPathSum(root.right,sum-root.val));
    }
}

 

posted on 2018-04-04 22:25  lina2014  阅读(180)  评论(0编辑  收藏  举报

导航