# 107. 二叉树的层次遍历 II

    3
/ \
9  20
/  \
15   7


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


# 解题思路以及代码

LeetCode 102. 二叉树的层序遍历

lists.add(0,list);

 static public List<List<Integer>> levelOrder(TreeNode root) {

List<List<Integer>> lists = new ArrayList<>();

if(root==null) return lists;

TreeNode treeNode = root;

while (!queue.isEmpty()){

int n = queue.size();
int index = 0;
List<Integer> list = new ArrayList<>();

while (index<n){
TreeNode node = queue.pop();

if(node.left!=null)

if(node.right!=null)

index++;

}

}

return lists;

}


package tree.二叉树的层次遍历2;

import java.util.ArrayList;
import java.util.List;
import java.util.Stack;

class Solution {

public static void main(String[] args) {
TreeNode t1 = new TreeNode(1);
TreeNode t2 = new TreeNode(2);
TreeNode t3 = new TreeNode(3);
TreeNode t4 = new TreeNode(4);
TreeNode t5 = new TreeNode(5);
t1.left=t2;
t1.right=t3;
t2.left=t4;
t2.right=t5;

System.out.println();
System.out.println("层次遍历：");
List<List<Integer>> lists = levelOrder(t1);

System.out.println("结果："+lists.toString());

System.out.println("遍历结果：");

lists.stream().forEach(li -> {

li.stream().forEach(ll -> {
System.out.print(ll.intValue()+" ");
});

System.out.println();

});

}

static public List<List<Integer>> levelOrder(TreeNode root) {

List<List<Integer>> lists = new ArrayList<>();

if(root==null) return lists;

TreeNode treeNode = root;

while (!queue.isEmpty()){

int n = queue.size();
int index = 0;
List<Integer> list = new ArrayList<>();

while (index<n){
TreeNode node = queue.pop();

if(node.left!=null)

if(node.right!=null)

index++;

}

}

return lists;

}
}

class TreeNode {
int val;//每个节点存放的数据
TreeNode left;//左节点
TreeNode right;//右节点
TreeNode(int x) { val = x; }
}



欢迎大佬们加入在下的小社，在此大家可以放开的交流，共同学习进步：

06-06 2096

11-12 44
06-23 2059
09-29 2604
06-21 2470
05-22 881
05-23 3136
02-04 300