Path Sum I:
Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all the values along the path equals the given sum.
For example:Given the below binary tree and
sum = 22, 5
/ \
4 8
/ / \
11 13 4
/ \ \
7 2 1
return true, as there exist a root-to-leaf path
5->4->11->2 which sum is 22.Analysis:
This is a basic practice for depth first search algorithm.The way of thinking this problem:
- This question requires only true/false return, so the final result is the "or" operation of all the possible root-to-leaf path.
- Parameters used in recursion should only be the sum value and the current node.
- Condition: if the sum == 0 and the node is leaf node return true,
- if the sum != 0 and the node is NULL, return false
- else return go root.left, sum-val || go root.right, sum-val
- How to check if the current node is a leaf node? Both its left and right children all empty.
Code(C++):
/**
* Definition for binary tree
* 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) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
if (root==NULL) {return false;}
if ( (root->left==NULL) && (root->right==NULL) && (sum-root->val==0) ) {return true;}
return (hasPathSum(root->left, sum-root->val) || hasPathSum(root->right, sum-root->val));
}
};
Code(Python):
# Definition for a binary tree node.
# class TreeNode(object):
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution(object):
def hasPathSum(self, root, sum):
"""
:type root: TreeNode
:type sum: int
:rtype: bool
"""
if not root:
return False
else:
sum = sum - root.val
if not root.left and not root.right:
if sum==0:
return True
return self.hasPathSum(root.left, sum) or self.hasPathSum(root.right, sum)
neat!
ReplyDelete