Problem Statement:
Implement In-order traversal using Stack for given Binary Tree.
Resultant Inorder Traversal using Stack , (with Space O(n))
Solution:
You can also do in-order traverse using recursion and Morris-Traversal using the concept of threaded binary trees.
Please comment and post your suggestion if you like the post or any improvisation needed in the code.
Happy Coding !! :)
Implement In-order traversal using Stack for given Binary Tree.
![]() |
Fig1: Given Binary Tree |
Resultant Inorder Traversal using Stack , (with Space O(n))
![]() |
Fig2: The in-order traversal of the given Binary Tree |
Solution:
Full Source Code: LINK
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | - /** *Inorder Traversal using Stack * @author Prateek */ public void inorderTravese(Node root) { if (root == null ) return ; Stack<Node> stack = new Stack<Node>(); Node current= root; while ( true ) { if (current!= null ) { stack.push(current); current = current.left ; } else { if (!stack.isEmpty()){ Node item = stack.pop() ; System.out.print(item + "\t" ); current=item.right; } else break ; } } } - |
You can also do in-order traverse using recursion and Morris-Traversal using the concept of threaded binary trees.
Please comment and post your suggestion if you like the post or any improvisation needed in the code.
Happy Coding !! :)
No comments:
Post a Comment