LeetCode103 BinaryTreeZigzagLevelOrderTraversal(二叉樹Z形層次遍曆) Java題解

來源:互聯網
上載者:User

標籤:zigzaglevelordertrav   binarytree   leetcode   java   

題目:

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  20    /     15   7

return its zigzag level order traversal as:

[  [3],  [20,9],  [15,7]]

解題:

這題其實和二叉樹的層序遍曆很類似  僅僅是在這個基礎上加上一個左右方向  一開始我的思維被局限住了  寫了一個很冗餘的代碼版本  實現思路是這樣的 當需要從右至左輸出的時候  我把隊列裡面的節點輸出然後 反序存回隊列

代碼:

public static List<List<Integer>> zigzagLevelOrder(TreeNode root) {List<List<Integer>> result=new ArrayList<List<Integer>>();//存放最終結果boolean isLeftToRight=false;//從左至右的方向Queue<TreeNode> nodeQueue=new LinkedList<>();//存放節點 便於每一層遍曆//處理根節點if(root==null)return result;else {List<Integer> 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<>();//用來暫時存放每一層節點的遍曆結果Stack<TreeNode> stack=new Stack<>();//用來輔助將隊列倒序if(isLeftToRight){//將隊列裡面的節點都先出隊列進棧再出棧進隊列  使得和原來的順序剛好相反for(int i=0;i<size;i++){stack.push(nodeQueue.poll());}while(!stack.isEmpty())nodeQueue.offer(stack.pop());while(size>0)//從左至右{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);//迴圈退出 表示一層已經遍曆完了  這時候重設方向標誌位isLeftToRight=false;}else {//將隊列裡面的節點都先出隊列進棧再出棧進隊列  使得和原來的順序剛好相反for(int i=0;i<size;i++){stack.push(nodeQueue.poll());}while(!stack.isEmpty())nodeQueue.offer(stack.pop());while(size>0)//從右至左{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);//迴圈退出 表示一層已經遍曆完了  這時候重設方向標誌位isLeftToRight=true;}}return result;            }

後面看別人的解答,其實完全沒有必要在隊列中反序  只要在存每一層輸出結果的ArrayList中反序存入就可以了  下面是這種思路的代碼:

public static List<List<Integer>> zigzagLevelOrder2(TreeNode root) {List<List<Integer>>  result=new ArrayList<List<Integer>>();//存放最終結果 Queue<TreeNode> nodeQueue=new LinkedList<>();//存放節點int flag=1;//flag為奇數的時候 從左至右  為偶數的時候從右至左;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)//從左至右{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;}}

看代碼的長度就知道我之前的有多複雜了 

著作權聲明:本文為博主原創文章,未經博主允許不得轉載。

LeetCode103 BinaryTreeZigzagLevelOrderTraversal(二叉樹Z形層次遍曆) Java題解

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.