LeetCode 112.. 路径总和
原题链接
简单
作者:
owonder
,
2020-09-07 12:20:47
,
所有人可见
,
阅读 502
C++ 代码
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
public:
bool hasPathSum(TreeNode* root, int sum) {
if(!root){
return false;
}
if(!root->left&&!root->right){
if(sum==root->val){
return true;
}else{
return false;
}
}
return hasPathSum(root->left, sum-root->val)||hasPathSum(root->right, sum-root->val);
}
};