/**
* 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 ok ;
int sumx;
void dfs(TreeNode*root,int sum){
if(root == NULL || ok) return ;
sum += root->val;
if(root->left == NULL && root->right == NULL && sum == sumx){
ok = true;
return ;
}
dfs(root->left,sum);
dfs(root->right,sum);
}
bool hasPathSum(TreeNode* root, int sum) {
ok = false;
if(root == NULL) return ok;
sumx = sum;
dfs(root,0);
return ok;
}
};
leetcode 112. Path Sum
最新推荐文章于 2024-07-12 18:12:33 发布