Monday, March 5, 2012

Spiral Level Order of a Binary Tree.


Following is a tree




You need to print
50 
76 17 
9 23 54 
72 19 14 
12 67 


Modified LEVEL order traversal.


Approach:
1. Use 2 stacks S1 and S2.
2. Keep track of levels and push left to right children on S1 and then pop S1 and push right to left children on S2.


Code input/output:
Printing inorder: 
9 12 14 17 19 23 50 54 67 72 76 
Printing spiral level order: 
50 
76 17 
9 23 54 
72 19 14 
12 67 

No comments:

Post a Comment