mthoutai

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理
/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
private:
    int indicator = 0;
public:
    void check(TreeNode *root, int sum) {
        if(root == NULL)
            return;
        if(root->left == NULL && root->right == NULL) {
            if(root->val == sum) 
                indicator = 1;
            return;
        }
        if(root->left == NULL) {
            check(root->right, sum - root->val);
            return;
        }
        if(root->right == NULL) {
            check(root->left, sum - root->val);
            return;
        }

        check(root->left, sum - root->val);
        check(root->right, sum - root->val);

    }
    bool hasPathSum(TreeNode* root, int sum) {
        check(root, sum);
        if(indicator == 1)
            return true;
        else
            return false;
    }
};
posted on 2017-06-17 15:49  mthoutai  阅读(266)  评论(0编辑  收藏  举报