Created
October 12, 2016 22:38
-
-
Save Roger7410/5fc463d264a71838249b98cde53be637 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class Solution { | |
| public List<List<Integer>> zigzagLevelOrder(TreeNode root) { | |
| List<List<Integer>> res = new ArrayList<List<Integer>>(); | |
| if(root == null) return res; | |
| Queue<TreeNode> queue = new LinkedList<>(); | |
| queue.add(root); | |
| boolean order = true; | |
| while(!queue.isEmpty()){ | |
| int size = queue.size(); | |
| List<Integer> tmpList = new ArrayList<>(); | |
| for(int i=0; i < size; i++){ | |
| TreeNode tmpNode = queue.poll(); | |
| if(order){ | |
| tmpList.add(tmpNode.val); | |
| }else{ | |
| tmpList.add(0,tmpNode.val); | |
| } | |
| if(tmpNode.left!=null) queue.add(tmpNode.left); | |
| if(tmpNode.right!=null) queue.add(tmpNode.right); | |
| } | |
| order = order ? false : true; | |
| res.add(tmpList); | |
| } | |
| return res; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment