This post is completed by 2 users
|
Add to List |
135. Find Paths with Sum in Binary Tree
Objective: - Given a binary tree and number S, write an algorithm to Print all the paths starting from the root so that the sum of all the nodes in the path equals a given number S.
Example:
Approach:
- Do the preorder traversal.
- take a variable path and pass it in each recursive call.
- if the root is greater than the Sum required, and return.
- If not then, add root to the path and update the required sum (sum=sum-root.data).
- if the sum required =0, means we have found the route, print the path.
- See the code for a better understanding.
1 2 7 1 3 6