AfterAcademy Tech
•
09 Jan 2020

Suppose you’re given an array filled with N elements. How will you traverse all N elements?
Why is this even a question, right? You will go from index 0 to index N-1 and thereby, traverse all elements of the array.
But what if you’re given the root of a tree? Can you traverse that tree? How will you traverse all elements of that tree?

There is a clear recursive structure in a tree data structure where we can solve a problem using the solution of its smaller subproblems. So we need to access three different elements in the tree:
We can access these three elements in six different ways i.e. there are 6 possible permutations. These are also called DFS traversal of a tree:
There are some other types of Non-Recursive traversals in trees that you can practice. We can use these traversals in the solution of several tree problems:
Can you think of some other ways of traversing elements in a tree?

In this blog, we are going to discuss the recursive and Iterative approach of following three traversals:
Preorder Traversal as we read above, traverse the tree in the order: Node, left child and then the right child.

Recursive Implementation
Tree traversals are naturally recursive in nature so let’s see its recursive implementation first →
void printPreorder(TreeNode root)
{
if ( root == NULL )
return
print ( root.val )
printPreorder( root.left )
printPreorder( root.right )
}
Simple to understand, right?
Well, let’s dive deeper now. In the recursion, the compiler does the stack maintenance for us. Let us try do it ourselves now.
Iterative implementation
Now we need to implement the stack ourselves. We need to perform the push() and pop() operations.
Solution Steps →
Now, what order did we follow in the above recursion? Print node, Recurse left and then recurse right. We shall follow the same order in iteration too.
4. In each iteration, print the node (i.e. the element at top of the stack) then push the right child in the stack and then the left child.
★ Why did we push the right child first? So that the left child gets to be on top and is processed first.
5. Don’t forget to pop the top node after printing it in each iteration.
Pseudo Code
void printPreorder(TreeNode root)
{
Create stack 'preorder' to fit elements of type TreeNode
preorder.push( root )
while ( preorder.empty() != True )
{
TreeNode node = preorder.peek()
preorder.pop()
if ( node == NULL )
continue
print ( node.val )
// Note that right child is pushed first
// so that left child is on top
preorder.push( node.right )
preorder.push( node.left )
}
}
Inorder Traversal follows the order LNR (left, node, right).

Recursive Implementation
void printInorder(TreeNode root)
{
if ( root == NULL )
return
printInorder( root.left )
print ( root.val )
printInorder( root.right )
}
Can you implement this iteratively now using stacks? Let’s try.
Iterative Implementation
Let’s form a strategy to approach the problem. Okay so first of all, we need to find the left-most node of our current tree. The leftmost node gets printed first. But what next? We now need the parent node and then the right sibling of this node. So we shall proceed in that manner and continue doing so until our stack is empty.
Solution Steps →
4. Repeat the process until all elements are processed
Pseudo Code
void printInorder(TreeNode root)
{
Create stack 'inorder' to fit elements of type TreeNode
while ( root != NULL or inorder.empty() != True )
{
// Find the leftmost node
while( root != NULL )
{
inorder.push(root)
root = root.left
}
root = inorder.peek()
inorder.pop()
print( root )
root = root.right
}
}
Postorder Traversal follows the LRN policy (left, right, node)

Recursive Implementation
void printPostorder(TreeNode root)
{
if ( root == NULL )
return
printPostorder(root.left)
printPostorder(root.right)
print ( root.val )
}
What about the iterative implementation? Can you try it yourself based on what you learnt in the above two iterative implementations?
Iterative Implementation
Let’s go through it in an organized manner. Let us assume we have a stack that prints postorder traversal of the tree when its elements are popped.
→ Which element do you suppose should be at the bottom-most position of such a stack? the root of tree (node is given the least preference)
→ And which element should be at the top-most position of this stack? the leftmost element in the tree(left child is given the highest priority)
So in order to create this stack, we should first push the root, then the right subtree and then the left subtree so that the left subtree is at the top of the stack. Same priority must be extended while pushing the individual subtrees.
Okay so now let’s design the solution steps keeping in mind the above priorities.
Solution Steps →
4. Now the postorder stack is created!
5. Pop and print the top element of the postorder stack.
6. Keep repeating step 5 until the postorder stack is empty.
Pseudo Code
void printPostorder(TreeNode root)
{
Create stack 'st' and 'postorder' to fit TreeNode elements
st.push(root)
while ( st.empty() != True )
{
root = st.peek()
st.pop()
if( root == NULL )
continue
postorder.push(root)
st.push(root.left)
st.push(root.right)
}
while( postorder.empty() != True )
{
TreeNode temp = postorder.peek()
print ( temp.val )
postorder.pop()
}
}
Happy Coding! Enjoy Algorithms!
AfterAcademy Tech
Given a binary tree, return the zigzag level order traversal of its nodes' values. (i.e, from left to right, then right to left for the next level and alternate between). The problem is a typical Interview problem based on Tree Traversal

AfterAcademy Tech
Given a binary tree and a sum, write a program to determine if the tree has a root-to-leaf path such that adding up all the values along the path equals the given sum.Its an easy problem based on Binary tree traversal and a famous interview question

AfterAcademy Tech
Find all nodes at a distance k from a target node. This problem requires the knowledge of tree data structure. Recursion and tree traversals would be the base of the solution for this problem.

AfterAcademy Tech
Given a binary tree, you need to compute the length of the diameter of the tree. The diameter of a binary tree is the length of the longest path between any two nodes in a tree. This path may or may not pass through the root.
