112 Path Sum
5
/ \
4 8
/ / \
11 13 4
/ \ \
7 2 1class Solution {
public:
bool isFound = false;
bool hasPathSum(TreeNode* root, int sum) {
hasPathSum_rec(root, sum);
return isFound;
}
void hasPathSum_rec(TreeNode *root, int sum) {
if (root && !isFound) {
sum = sum - root->val;
if (!root->left && !root->right && sum == 0)
isFound = true;
else {
hasPathSum(root->left, sum);
hasPathSum(root->right, sum);
}
}
}
};Previous105 Construct Binary Tree from Preorder and Inorder TraversalNext114 Flatten Binary Tree to Linked List
Last updated