Skip to content

Commit

Permalink
Java solution 102
Browse files Browse the repository at this point in the history
  • Loading branch information
gavinfish committed Jun 7, 2017
1 parent e5aca26 commit 4a07830
Showing 1 changed file with 52 additions and 0 deletions.
52 changes: 52 additions & 0 deletions java/_102BinaryTreeLevelOrderTraversal.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import java.util.ArrayList;
import java.util.List;

/**
* Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, level by level).
* <p>
* For example:
* Given binary tree [3,9,20,null,null,15,7],
* 3
* / \
* 9 20
* / \
* 15 7
* <p>
* return its level order traversal as:
* [
* [3],
* [9,20],
* [15,7]
* ]
* <p>
* Created by drfish on 29/05/2017.
*/
public class _102BinaryTreeLevelOrderTraversal {
public List<List<Integer>> levelOrder(TreeNode root) {
List<List<Integer>> result = new ArrayList<>();
List<TreeNode> curr = new ArrayList<>();


if (root == null) {
return result;
}
curr.add(root);

while (!curr.isEmpty()) {
List<TreeNode> next = new ArrayList<>();
List<Integer> level = new ArrayList<>();
for (TreeNode node : curr) {
if (node.left != null) {
next.add(node.left);
}
if (node.right != null) {
next.add(node.right);
}
level.add(node.val);
}
result.add(level);
curr = next;
}
return result;
}
}

0 comments on commit 4a07830

Please sign in to comment.