Topic:
Given a binary tree, return the zigzag level order traversal of its nodes ' values. (ie, from left-to-right, then right-to-left for the next level and alternate between).
For example:
Given binary Tree {3,9,20,#,#,15,7} ,
3 / 9 / 7
Return its zigzag level order traversal as:
[ 3], [20,9], [15,7]]
Solving:
In fact, the sequence traversal of the two-fork tree is very similar, but on this basis, with a left-right direction, my mind is confined to writing a very redundant version of the code to implement the idea is that when you need to output from the left to the back, I put the node output in the queue and then reverse-order to the queue
Code:
public static list<list<integer>> Zigzaglevelorder (TreeNode root) {list<list<integer>> Result=new arraylist<list<integer>> ();//Storage finally results in a Boolean islefttoright=false;//from left to right Queue<treenode > Nodequeue=new linkedlist<> ()///Storage node facilitates traversal//processing of root node if (root==null) return result;else {list<integer> in each layer List=new arraylist<> (); List.add (Root.val); Result.add (list); Nodequeue.offer (root);} while (!nodequeue.isempty ()) {int size=nodequeue.size (); List<integer> tempresult=new arraylist<> ()///used to temporarily store the traversal results for each layer of nodes stack<treenode> stack=new Stack< > ();//used to assist in reverse queue if (islefttoright) {//The nodes inside the queue are first out of the queue and then out of the stack into the queue so that the original order is exactly the opposite for (int i=0;i<size;i++) {Stack.push ( Nodequeue.poll ());} while (!stack.isempty ()) Nodequeue.offer (Stack.pop ()), while (size>0)//From left to right {size--; TreeNode Tempnode=nodequeue.poll (); if (tempnode.left!=null) {nodequeue.offer (tempnode.left); Tempresult.add ( TempNode.left.val);} if (tempnode.right!=null) {nodequeue.offer (tempnode.right); tempresult.aDD (tempNode.right.val);}} if (!tempresult.isempty ()) Result.add (tempresult);//Loop exit indicates that a layer has been traversed this time reset direction flag bit islefttoright=false;} else {//the nodes inside the queue are first out of the queue and then out of the stack into the queue so that the original order is exactly the opposite for (int i=0;i<size;i++) {Stack.push (Nodequeue.poll ());} while (!stack.isempty ()) Nodequeue.offer (Stack.pop ()), while (size>0)//right-to-left {size--; TreeNode Tempnode=nodequeue.poll (); if (tempnode.right!=null) {nodequeue.offer (tempnode.right); Tempresult.add ( TempNode.right.val);} if (tempnode.left!=null) {nodequeue.offer (tempnode.left); Tempresult.add (TempNode.left.val);}} if (!tempresult.isempty ()) Result.add (tempresult);//Loop exit indicates that a layer has been traversed this time reset direction flag bit islefttoright=true;} return result; }
Looking back at someone else's answer, in fact there is absolutely no need to reverse order in the queue only in the ArrayList of the output of each layer in the reverse order to save the following is the idea of the code:
public static list<list<integer>> ZigzagLevelOrder2 (TreeNode root) {list<list<integer>> result=new arraylist<list<integer>> ();//Storage finally results queue<treenode> nodequeue=new LinkedList< > ()////Store node int flag=1;//flag is odd when left to right is even from right to left. if (root==null) return Result;else {nodequeue.add (root);} while (!nodequeue.isempty ()) {int count=nodequeue.size (); List<integer> tempresult=new arraylist<> (); while (count>0) {TreeNode tempnode=nodequeue.poll (); Count--;if (flag%2!=0)//From left to right {Tempresult.add (tempnode.val);} else {//tempresult.add (0,tempnode.val);} if (tempnode.left!=null) Nodequeue.add (Tempnode.left), if (tempnode.right!=null) Nodequeue.add (tempNode.right);} if (!tempresult.isempty ()) Result.add (tempresult); flag++;} return result;}}
Look at the length of the code and you know how complicated I was.
LeetCode103 binarytreezigzaglevelordertraversal (binary tree Z-level traversal) Java puzzle