码迷,mamicode.com
首页 > 其他好文 > 详细

Leetcode Path Sum

时间:2014-10-08 01:05:04      阅读:253      评论:0      收藏:0      [点我收藏+]

标签:blog   http   io   os   ar   for   sp   c   on   

/**
 * 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

标签:blog   http   io   os   ar   for   sp   c   on   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!