美文网首页
112. Path Sum

112. Path Sum

作者: 刘小小gogo | 来源:发表于2018-08-20 22:57 被阅读0次
    image.png
    /**
     * 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 {
    public:
        bool hasPathSum(TreeNode* root, int sum) {
            if(root == NULL) return false;
            if(root->val == sum && root->left == NULL && root->right == NULL  ) return true;
            return hasPathSum(root->left, sum - root->val) || hasPathSum(root->right, sum - root->val);
        }
    };
    

    相关文章

      网友评论

          本文标题:112. Path Sum

          本文链接:https://www.haomeiwen.com/subject/xzpriftx.html