LeetCode103 BinaryTreeZigzagLevelOrderTraversal (Binary Tree Z-level traversal) Java question
Question:
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 ).
For example:
Given binary tree{3,9,20,#,#,15,7},
3 / 9 20 / 15 7
Return its zigzag level order traversal:
[ [3], [20,9], [15,7]]
Solution:
In fact, this is similar to the sequence traversal of Binary Trees. Just on this basis, I added a left and right direction. At the beginning, my thinking was limited. I wrote a very redundant code version implementation idea. when I need to output data from the right to the left, I output the nodes in the queue and store the back-to-back columns in reverse order.
Code:
Public static List
> ZigzagLevelOrder (TreeNode root) {List
> Result = new ArrayList
> (); // Stores the final result boolean isLeftToRight = false; // Queue from left to right
NodeQueue = new partition List <> (); // store nodes to traverse each layer. // process the root node if (root = null) return result; else {List
List = new ArrayList <> (); list. add (root. val); result. add (list); nodeQueue. offer (root);} while (! NodeQueue. isEmpty () {int size = nodeQueue. size (); List
TempResult = new ArrayList <> (); // It is used to temporarily store the traversal result Stack of each layer of nodes.
Stack = new Stack <> (); // used to aid in the reverse order of the queue if (isLeftToRight) {// send all the nodes in the queue to the stack first and then to the queue so that the order is exactly the opposite to that of the original for (int I = 0; I
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); // indicates that the layer has been traversed. At this time, the reset Direction Flag isLeftToRight = false ;} else {// put all the nodes in the queue into the stack first and then the stack into the queue so that the order is just the opposite (int I = 0; I
0) // from 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 the layer has been traversed. At this time, the reset Direction Flag isLeftToRight = true;} return result ;}
Looking at other people's answers, there is no need to store the reverse order in the queue as long as the ArrayList storing each layer of output results is saved in reverse order. The following is the code of this idea:
Public static List
> ZigzagLevelOrder2 (TreeNode root) {List
> Result = new ArrayList
> (); // Stores the final result Queue
NodeQueue = new shard list <> (); // storage node int flag = 1; // when the flag is an odd number, it is left to right to left; if (root = null) return result; else {nodeQueue. add (root);} while (! NodeQueue. isEmpty () {int count = nodeQueue. size (); List
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 ;}}
Looking at the code length, we can see how complicated I was before.