Problem
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.
A leaf is a node with no children.
Examples
Example 1:
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
| |
Example 2:
| |
| |
Example 3:
| |
Solution
Method 1 - Using Recursion
Code
| |
Method 2 - Using Queue
Code
| |