Show Buttons
Share On Facebook
Share On Twitter
Share On Google Plus
Share On Linkdin
Share On Pinterest
Share On Reddit
Share On Stumbleupon
Contact us
Hide Buttons

Reverse Alternate levels of a given Binary Tree.

Objec­tive: - Reverse Alter­nate lev­els of a given binary tree

Input: A binary tree

Exam­ple:

Reverse Alternate levels of a given binary tree.

Reverse Alter­nate lev­els of a given binary tree.

Appraoch:

  • Do the inorder tra­ver­sal and store all the alter­nate level nodes in an ArrayList.
  • Reverse the ArrayList
  • Do another inorder tra­ver­sal and place the reversed array list in the same order in which it was fetched in step one.
  • Look at the code for clear explanation.

Com­plete Code:


Out­put:
Orin­i­nal Tree

1
2 3
4 5 6 7
8 9 10 11 12 13 14 15

New Tree, Alternate Levels Reversed..
1
3 2
4 5 6 7
15 14 13 12 11 10 9 8

You may also like...