For the below example tree, all root-to-leaf paths are:
10 –> 8 –> 3
10 –> 8 –> 5
10 –> 2 –> 2
Use a path array path to store current root to leaf path. Traverse from root to all leaves in top-down fashion. While traversing, store data of all nodes in current path in array path. When we reach a leaf node, print the path array.
10 8 3 10 8 5 10 2 2
Time Complexity: O(n2) where n is number of nodes.
Please write comments if you find any bug in above codes/algorithms, or find other ways to solve the same problem.
- Print all k-sum paths in a binary tree
- Print all the paths from root, with a specified sum in Binary tree
- Given a binary tree, print out all of its root-to-leaf paths one per line.
- Root to leaf paths having equal lengths in a Binary Tree
- Print Right View of a Binary Tree
- Print Binary Tree in 2-Dimensions
- Print all even nodes of Binary Search Tree
- Print Levels of all nodes in a Binary Tree
- Print nodes in the Top View of Binary Tree | Set 3
- Print nodes in top view of Binary Tree | Set 2
- Print Nodes in Top View of Binary Tree
- Print a Binary Tree in Vertical Order | Set 1
- Print Left View of a Binary Tree
- Print all nodes in a binary tree having K leaves
- Print all nodes between two given levels in Binary Tree
Improved By : shweta44