Untitled
unknown
plain_text
5 months ago
878 B
5
Indexable
public static void zigzagTraversal(Node root){ Stack<Node> st1 = new Stack<>(); Stack<Node> st2 = new Stack<>(); st1.push(root); boolean flag = true; while(!st1.isEmpty()){ Node temp = st1.pop(); System.out.println(temp.data); if(flag) { if(temp.left != null){ st2.push(temp.left); } if(temp.right != null){ st2.push(temp.right); } } else { if(temp.right != null) { st2.push(temp.right); } if (temp.left != null){ st2.push(temp.left); } } if(st1.isEmpty()){ flag = !flag; Stack<Node> temp1 = st1; st1 = st2; st2 = temp1; } } }
Editor is loading...
Leave a Comment