Challenge: Print All the Paths
Let’s print all the paths from the root node to the leaf node.
We'll cover the following
Problem
Given a binary tree, print all the paths from the root to the leaves.
Input
The root of a binary tree.
Output
Display all paths in the binary tree.
Sample input
The root node t.root
of a binary tree will be the input of printAllPath()
function. We have to print all the paths of the given tree.
Here’s the sample input tree t
.
t := LevelOrderBinaryTree(arr)
Sample output
All paths printed to the console using a semicolon (;
) separated approach instead of line breaks.
7 3 1 ; 6 3 1 ; 10 5 2 1 ; 9 4 2 1 ; 8 4 2 1
Note: Please add space after a semicolon too in order to successfully complete the exercise.
Let’s look at the illustration below to better understand the problem.
Create a free account to view this lesson.
By signing up, you agree to Educative's Terms of Service and Privacy Policy