This post is completed by 1 user

  • 0
Add to List
Medium

75. Print the Binary Tree in Vertical Order Path

Objective: - Given a binary tree, print it in vertical order path.

What is Vertical Order

as you can see in the example above, [4],[2], [1,5,6],[3],[7] are the vertical order of the given binary tree.

Approach:

  • Do the preorder traversal.
  • Take a variable called level, whenever you go left, do level++ AND whenever you go right do level--.
  • With the step above we have separated the levels vertically.
  • Now you need to store the elements of each level, so create a map or dictionary to store the level as key and elements at that level as value.
  • At the end iterate through the dictionary and print the results.
Separate out the vertical levels
 

Output:

[4]
[2]
[1, 5, 6]
[3]
[7]