# Binary Tree Level Order Traversal

Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, level by level).

For example:
Given binary tree [3,9,20,null,null,15,7],

    3
/ \
9  20
/  \
15   7


return its level order traversal as:

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

public class Solution {
public List<List<Integer>> levelOrder(TreeNode root) {
int depth = 1;
List<List<Integer>> lst = new ArrayList<List<Integer>>();
levelOrder(lst,depth,root);
return lst;
}
public void levelOrder(List<List<Integer>> lst,int depth,TreeNode root){
if(root == null){
return;
}else{
if(lst.size() < depth){
List<Integer> lsIn = new ArrayList<Integer>();
}
List<Integer> ls = lst.get(depth - 1);
if(root.left != null){
levelOrder(lst,depth+1,root.left);
}
if(root.right != null){
levelOrder(lst,depth+1,root.right);
}

}

}
}

public class Solution {
public List<List<Integer>> levelOrder(TreeNode root) {
int cnt = 0;
int f = 0;
int t = 1;
List<List<Integer>> lsts = new ArrayList<List<Integer>>();
List<Integer> lst = new ArrayList<Integer>();
List<TreeNode> nodeList = new ArrayList<TreeNode>();
if(root == null){
return 	lsts;
}
while(t != 0){
if(f == t){
t = cnt;
f = 0;
cnt = 0;
lst = new ArrayList<Integer>();
}
else{
TreeNode node = nodeList.get(0);
if(node != null){
f ++;
}
if(node.left != null){
cnt ++;
}
if(node.right != null){
cnt ++;
}
nodeList.remove(node);
}
}
return 	lsts;
}
}

public List<List<Integer>> levelOrder(TreeNode root) {
int cnt = 0;
int f = 0;
int t = 1;
List<List<Integer>> lsts = new ArrayList<List<Integer>>();
List<Integer> lst = new ArrayList<Integer>();
List<TreeNode> nodeList = new ArrayList<TreeNode>();
if(root == null){
return 	lsts;
}
while(t != 0){
if(f == t){
t = cnt;
f = 0;
cnt = 0;
lst = new ArrayList<Integer>();
}
else{
TreeNode node = nodeList.get(0);
if(node != null){
f ++;
}
if(node.left != null){
cnt ++;
}
if(node.right != null){
cnt ++;
}
nodeList.remove(0);
}
}
return 	lsts;
}

public List<List<Integer>> levelOrder(TreeNode root) {
int cnt = 0;
int f = 0;
int t = 1;
int index;
List<List<Integer>> lsts = new ArrayList<List<Integer>>();
List<Integer> lst = new ArrayList<Integer>();
List<TreeNode> nodeList = new ArrayList<TreeNode>();
if(root == null){
return 	lsts;
}
index = 0;
while(t != 0){
if(f == t){
t = cnt;
f = 0;
cnt = 0;
lst = new ArrayList<Integer>();
}
else{
TreeNode node = nodeList.get(index ++);
if(node != null){
f ++;
}
if(node.left != null){
cnt ++;
}
if(node.right != null){
cnt ++;
}
}
}
return 	lsts;
}

• 本文已收录于以下专栏：

举报原因： 您举报文章：Binary Tree Level Order Traversal 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)