首页 > 其他 > 详细

Leetcode Path Sum

时间:2014-10-07 23:48:04      阅读:284      评论:0      收藏:0      [点我收藏+]
/**
 * Definition for binary tree
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
 /**
 * https://oj.leetcode.com/problems/path-sum/
 * 参考http://www.cnblogs.com/remlostime/archive/2012/11/13/2767746.html
 */
 

 
class Solution {
public:
    bool hasPathSum(TreeNode *root, int sum) {
        return hasSum(root, sum, 0);
    }
    
    bool hasSum(TreeNode *node, int sum, int curSum){
        
        if(node == NULL){
            return false;
        }
        
        if(!node->left && !node->right){
            return (sum == curSum + node->val);
        }
        
        return hasSum(node->left, sum, curSum + node->val) || hasSum(node->right, sum, curSum + node->val);
    }
};

Leetcode Path Sum

原文:http://blog.csdn.net/wyj7260/article/details/39861815

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