Zigzag Traversal of Binary Tree
How to traverse Binary Tree to print out Zigzag.
This is one of popular interview question of Data structure written in Java to accomplish to zigzag layout.
Here, We create a Node having left and right node that point to backward and forward Node.
There are three way to traverse Tree :-
Below are Algorithm and Java Code for traversing methods:
Inorder :-
Code : -
Code :-
Here, Implementation of ZigZag using two stack:-
Below are complete code:-
output :-
10 20 30 40 50 60 70 80 90 95 65 55 66 35 45
There are three way to traverse Tree :-
- In-Order -
- Traverse from root
- then, left
- then, right
- Pre-Order -
- Traverse from left
- then, root node
- then right node
- Post-Order -
- Traverse from left
- then right
- then left
Below are Algorithm and Java Code for traversing methods:
Inorder :-
inorder(node) if node == null then return inorder(node.left) visit(node) inorder(node.right)
Code : -
public static void inOrderTraverse(Node root){ if(root != null){ System.out.printf("%d ",root.data); inOrderTraverse(root.left); inOrderTraverse(root.right); } }preOrder :-
preorder(node) if node == null then return visit(node) preorder(node.left) preorder(node.right)
Code :-
public static void preOrderTraverse(Node root){ if(root != null){ preOrderTraverse(root.left); System.out.printf("%d ",root.data); preOrderTraverse(root.right); } }postOrder :-
postorder(node) if node == null then return postorder(node.left) postorder(node.right) visit(node)Code -
public static void postOrderTraverse(Node root){ if(root != null){ postOrderTraverse(root.left); postOrderTraverse(root.right); System.out.printf("%d ",root.data); } }We assume that below is our tree-
class Node { Node left; Node right; int data; Node(int value) { this.data = value; } }
Here, Implementation of ZigZag using two stack:-
public static void printZigZagTree(Node root) { Stackstack = new Stack (); stack.push(root); boolean flags = false; while (!stack.isEmpty()) { Stack tempStack = new Stack (); while (!stack.isEmpty()) { Node node = stack.pop(); System.out.println(node.data); if (!flags) { if (node.right != null) { tempStack.push(node.right); } if (node.left != null) { tempStack.push(node.left); } } else { if (node.left != null) { tempStack.push(node.left); } if (node.right != null) { tempStack.push(node.right); } } } flags = !flags; stack = tempStack; } }
Below are complete code:-
package com.DS; import java.util.Stack; class Node { Node left; Node right; int data; Node(int value) { this.data = value; } } /** * @author ajay kumar gupta * */ public class ZigZagDB { public static void main(String args[]) { Node root = getTreeRoot(); System.out.println("ZigZag Traverse :- "); printZigZagTree(root); System.out.println("\n InOrder Traverse :- "); inOrderTraverse(root); System.out.println("\n PreOrder Traverse :- "); preOrderTraverse(root); System.out.println("\n PostOrder Traverse :- "); postOrderTraverse(root); } public static void inOrderTraverse(Node root){ if(root != null){ System.out.printf("%d ",root.data); inOrderTraverse(root.left); inOrderTraverse(root.right); } } public static void preOrderTraverse(Node root){ if(root != null){ preOrderTraverse(root.left); System.out.printf("%d ",root.data); preOrderTraverse(root.right); } } public static void postOrderTraverse(Node root){ if(root != null){ postOrderTraverse(root.left); postOrderTraverse(root.right); System.out.printf("%d ",root.data); } } public static void printZigZagTree(Node root) { Stackstack = new Stack (); stack.push(root); boolean flags = false; while (!stack.isEmpty()) { Stack tempStack = new Stack (); while (!stack.isEmpty()) { Node node = stack.pop(); System.out.printf("%d ",node.data); if (!flags) { if (node.right != null) { tempStack.push(node.right); } if (node.left != null) { tempStack.push(node.left); } } else { if (node.left != null) { tempStack.push(node.left); } if (node.right != null) { tempStack.push(node.right); } } } flags = !flags; stack = tempStack; } } public static Node getTreeRoot() { Node root10 = new Node(10); Node node20 = new Node(20); Node node30 = new Node(30); Node node40 = new Node(40); Node node50 = new Node(50); Node node60 = new Node(60); Node node70 = new Node(70); Node node80 = new Node(80); Node node90 = new Node(90); Node node95 = new Node(95); Node node65 = new Node(65); Node node55 = new Node(55); Node node66 = new Node(66); Node node35 = new Node(35); Node node45 = new Node(45); root10.right = node30; root10.left = node20; node20.right = node60; node20.left = node70; node30.right = node40; node30.left = node50; node70.right = node90; node70.left = node80; node60.right = node65; node60.left = node95; node50.right = node66; node50.left = node55; node40.right = node45; node40.left = node35; return root10; } }
output :-
10 20 30 40 50 60 70 80 90 95 65 55 66 35 45