首页 > 其他 > 详细

LeetCode Path Sum

时间:2014-03-24 19:46:40      阅读:493      评论:0      收藏:0      [点我收藏+]
bubuko.com,布布扣
/**
 * Definition for binary tree
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    bool hasPathSum(TreeNode *root, int sum) {
        return dfs(root, 0, sum);
    }
    
    bool dfs(TreeNode* root, int cur_sum, int target) {
        if (root == NULL) return false;
        cur_sum = cur_sum + root->val;
        if (cur_sum == target && root->left == NULL && root->right == NULL) return true;
        if (dfs(root->left, cur_sum, target)) return true;
        if (dfs(root->right, cur_sum, target)) return true;
        return false;
    }
};
bubuko.com,布布扣

水一发

LeetCode Path Sum,布布扣,bubuko.com

LeetCode Path Sum

原文:http://www.cnblogs.com/lailailai/p/3621099.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!