First, normal fasion
Use queue record last accessed child node of record
1 PublicList<list<integer>>Levelorder (TreeNode root) {2list<list<integer>> res =NewLinkedlist<list<integer>>();3queue<treenode> queue =NewLinkedlist<treenode>();4 Queue.add (root);5 while(!Queue.isempty ()) {6 intSize=queue.size ();7List<integer> Listone =NewLinkedlist<integer>();8 for(inti=0;i<size;i++){9TreeNode tmp =Queue.poll ();Ten Listone.add (tmp.val); One if(tmp.left!=NULL) Queue.offer (tmp.left); A if(tmp.right!=NULL) Queue.offer (tmp.right); - } - Res.add (listone); the } - returnRes; -}
Second, zigzag fasion
Use two stacks to alternately record your child's information and use the index flag to traverse from left to right or from right to left, both of which affect the order in which your child's nodes traverse
1 PublicList<list<integer>>Zigzaglevelorder (TreeNode root) {2list<list<integer>> res =NewLinkedlist<list<integer>>();3Stack [] Stacks =NewStack[2];4stacks[0]=NewStack ();5stacks[1]=NewStack ();6 intIndex=0;7 while(!stacks[index%2].isempty ()) {8stack<integer> S1 = stacks[index%2];9stack<integer> s2 = stacks[(index+1)%2];TenList<integer> Listone =NewLinkedlist<integer>(); One while(!S1.isempty ()) { ATreeNode tmp =S1.pop (); - Listone.add (tmp.val); - if(index%2==0){ the if(s1.left!=NULL) S2.push (s1.left); - if(s1.right!=NULL) S2.push (s1.right); -}Else{ - if(s1.right!=NULL) S2.push (s1.right); + if(s1.left!=NULL) S2.push (s1.left); - } + } A Res.add (listone); atindex= (index+1)%2; - } - returnRes; -}
Three, bottom up fasion
Both of these methods are actually the normal method, but the last one is to reverse the layers in the list.
3.1 Almost the same way
1 PublicList<list<integer>>levelorderbottom (TreeNode root) {2list<list<integer>> res =NewLinkedlist<list<integer>>();3queue<treenode> queue =NewLinkedlist<treenode>();4 Queue.offer (root);5 while(!Queue.isempty ()) {6List<integer> Listone =NewLinkedlist<integer>();7 intSize=queue.size ();8 for(int=0;i<size;i++){9TreeNode tmp =Queue.poll ();Ten Listone.add (tmp.val); One if(tmp.left!=NULL) Queue.offer (tmp.left); A if(tmp.right!=NULL) Queue.offer (tmp.right); - } - Res.add (listone); the } - //reverse them+ for (int i=0;i<res.size ()/2;i++) { list<integer> tmp = Res.get (i); Res.set (I,res.get (Res.size ()-i)); Res.set (res.size ()-i,tmp); + } A returnRes; at}
3.2 Using recursion
1 //using DFS method to store the result of each2 Private voidDFS (list<list<integer>>res, TreeNode root,intLevel ) {3 if(Res.size () <Level ) {4List<integer> Levelone =NewLinkedlist<integer>();5 Res.add (levelone);6 }7list<integer> Levelone = Res.get (level-1);8 Levelone.add (root.val);9DFS (res,root.left,level+1);TenDFS (res,root.right,level+1); One } A PublicList<list<integer>>levelorderbottom (TreeNode root) { -list<list<integer>> res =NewLinkedlist<list<integer>>(); -DFS (res,root,1); the //reverse them - for(intI=0;i<res.size ()/2;i++){ -list<integer> tmp =Res.get (i); -Res.set (I,res.get (Res.size ()-1-i)); +Res.set (Res.size ()-1-i,tmp); - } + returnRes; A}
Problem:
Is there a way to use reverse?
[Leetcode] Binary Tree Level order travelsal (normal and zigzag and bottom-up)