Created
August 28, 2017 21:06
-
-
Save cixuuz/ff9aeaeae69f6b675798f6d12a4e5dc5 to your computer and use it in GitHub Desktop.
[107. Binary Tree Level Order Traversal II] #leetcode
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
class Solution { | |
public List<List<Integer>> levelOrderBottom(TreeNode root) { | |
List<List<Integer>> res = new LinkedList<List<Integer>>(); | |
if (root == null) return res; | |
Deque<TreeNode> deque = new LinkedList<TreeNode>(); | |
deque.addLast(root); | |
while ( !deque.isEmpty() ) { | |
int n = deque.size(); | |
List<Integer> currentLevel = new ArrayList<Integer>(); | |
for ( int i = 0 ; i < n ; i++ ) { | |
TreeNode node = deque.removeFirst(); | |
currentLevel.add(node.val); | |
if ( node.left != null ) deque.addLast(node.left); | |
if ( node.right != null ) deque.addLast(node.right); | |
} | |
res.add(0, currentLevel); | |
} | |
return res; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment