This post is completed by 1 user
|
Add to List |
59. Print All Root-to-Leaf Paths in a Binary Tree
Objective: Given a binary tree, Print paths from the root to all leaf nodes.
Note: 0<Nodes in tree <=100
Example:
Approach:
- Do the pre-order traversal.
- Store the current node in an array, say path[], and maintain the length say pathLength.
- check if you are at the leaf node (both the left and right child of the parent will be null).
- If not then keep traversing by making recursive calls to root.left and root.right. Pass the path and pathLen as a parameter so that each level has its copy of the path and its length.
- If you hit the leaf node then print the array.
1 2 4 7 1 3 6 8 1 3 6 9