Given the root of a binary tree and an integer targetSum, return true if the tree has a root-to-leaf path such that adding up all the values along the path equals targetSum.
graph TD;
A[5] --> B[4] & C[8]
B --> D[11] & E[null]
C --> F[13] & G[4]
D --> H[7] & I[2]
G --> L[5] & M[1]
style A fill:#f9f
style B fill:#f9f
style D fill:#f9f
style I fill:#f9f
1
2
3
Input: root =[5,4,8,11,null,13,4,7,2,null,null,null,1], targetSum =22Output: trueExplanation: Following root-to-leaf path exists:[5,4,11,2], marked with() tree. There can be multiple paths, but we have to just return boolean value for that.
Example 2:
1
2
3
1/ \
23
1
2
3
4
5
6
Input: root =[1,2,3], targetSum =5Output: falseExplanation: There two root-to-leaf paths in the tree:(1-->2): The sum is3.(1-->3): The sum is4.There is no root-to-leaf path with sum =5.
Example 3:
1
2
3
Input: root = [], targetSum =0Output: false
Explanation: Since the tree is empty, there are no root-to-leaf paths.