Path Sum

/**
 * 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;
    }
};
原文地址:https://www.cnblogs.com/mthoutai/p/7040411.html