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:
This question is actually similar to the sequence traversal of the two-fork tree, just on this basis, with a left and right direction. My mind was confined to the idea of writing a very redundant version of the code. When you need to output from right to left, I put the node output in the queue back into the queue.
Code:
public static list<list<integer>> Zigzaglevelorder (TreeNode root) {list<list<integer>> Result=new arraylist<list<integer>> ();//Store final result Boolean islefttoright=false;//direction 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; }
After looking at other people's answers, in fact, there is absolutely no need to reverse order in the queue as long as the output of each layer in the ArrayList of the results of the reverse order to save the following is the code of this idea:
public static list<list<integer>> ZigzagLevelOrder2 (TreeNode root) {list<list<integer>> result=new arraylist<list<integer>> ();//store final result queue<treenode> nodequeue=new LinkedList< > ()///Flag=1;//flag the node int is odd when left to right is an even number 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.
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
LeetCode103 binarytreezigzaglevelordertraversal (binary tree Z-level traversal) Java puzzle