112. Path Sum

来源:互联网 发布:怎么发淘宝优惠券赚钱 编辑:程序博客网 时间:2024/05/21 03:55
/** * 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) return false;        if(!root->left&&!root->right&&root->val==sum) return true;        sum-=root->val;        if(hasPathSum(root->left,sum)) return true;        if(hasPathSum(root->right,sum)) return true;        return false;    }};
0 0
原创粉丝点击