Given a Binary Tree, the task is to print the zigzag order of the tree.
Input : 7 / \ 6 5 / / 4 3 / \ 2 1 Output : 7 5 6 4 3 1 2 Input : 1 / \ 2 3 / \ 4 5 Output : 1 3 2 4 5
We have already discussed the zigzag traversal using an iterative approach, in this post we will solve it using Recursion.
Recursive Approach: The idea is to traverse the tree in a Level Order manner but in a slightly different manner. We will use a variable flag and initially set it’s value to zero. As we complete the level order traversal of the tree, from right to left we will set the value of flag to one, so that next time we can traverse the Tree from left to right and as we complete the traversal we will set it’s value back to zero. We will repeat this whole step until we have traversed the Binary Tree completely.
Below is the implementation of the above approach:
7 5 4 9 10 11 6
- Reverse zigzag Traversal of a Binary Tree
- Postorder traversal of Binary Tree without recursion and without stack
- Inorder Non-threaded Binary Tree Traversal without Recursion or Stack
- ZigZag Tree Traversal
- DFS traversal of a tree using recursion
- Preorder Traversal of N-ary Tree Without Recursion
- Inorder Tree Traversal without Recursion
- Inorder Tree Traversal without recursion and without stack!
- Construct Full Binary Tree using its Preorder traversal and Preorder traversal of its mirror tree
- Given level order traversal of a Binary Tree, check if the Tree is a Min-Heap
- Diagonal Traversal of Binary Tree
- Density of Binary Tree in One Traversal
- Boundary Traversal of binary tree
- Leaf nodes from Preorder of a Binary Search Tree (Using Recursion)
- Clockwise Spiral Traversal of Binary Tree