simpleLevelOrder广度优先遍历
levelOrder返回每层的列表
levelOrderDESC倒序返回每层的列表
zigzagLevelOrder交叉逆序返回每层列表
nLevelOrder返回n叉树的每层列表
import java.util.*;
public class LevelOrder {
public static void main(String[] args) {
Node root = new Node(3);
Node n2 = new Node(9);
Node n3 = new Node(20);
Node n4 = new Node(15);
Node n5 = new Node(7);
root.left = n2;root.right=n3;n3.left=n4;n3.right=n5;
System.out.println(simpleLevelOrder(root));
System.out.println(levelOrder(root));
System.out.println(levelOrderDesc(root));
System.out.println(zigzagLevelOrder(root));
NNode nroot = new NNode(1);
NNode nn2 = new NNode(3);
NNode nn3 = new NNode(2);
NNode nn4 = new NNode(4);
NNode nn5 = new NNode(5);
NNode nn6 = new NNode(6);
nroot.children= new ArrayList<>(){{add(nn2);add(nn3);add(nn4);}};
nn2.children=new ArrayList<>(){{add(nn5);add(nn6);}};
System.out.println(nLevelOrder(nroot));
}
public static List<List<Integer>> nLevelOrder(NNode root){
List<List<Integer>> res = new LinkedList<>();
Queue queue = new LinkedList<NNode>();
queue.offer(root);
while (queue.size()>0){
int size = queue.size();
List<Integer> level = new LinkedList<>();
for (;size>0;size--){
NNode t = (NNode) queue.remove();
List<NNode> children= t.children;
if (children!=null){
children.stream().forEach(i->{
queue.offer(i);
});
}
level.add(t.value);
}
res.add(level);
}
return res;
}
public static List<List<Integer>> zigzagLevelOrder(Node root){
if (root==null){
return new ArrayList<>();
}
List<List<Integer>> res = new ArrayList<>();
Queue<Node> queue = new LinkedList<>();
queue.offer(root);
boolean desc = false;
while (queue.size()>0){
int size = queue.size();
List<Integer> level = new ArrayList<>();
for (;size>0;size--){
Node t = queue.poll();
Node left = t.left;
Node right = t.right;
if (left!=null){
queue.add(left);
}
if (right!=null){
queue.add(right);
}
if (desc) {
level.add(0,t.value);
}else {
level.add(t.value);
}
}
res.add(level);
desc=!desc;
}
return res;
}
public static List<List<Integer>> levelOrderDesc(Node root){
if (root==null){
return new ArrayList<>();
}
List<List<Integer>> res = new ArrayList<>();
Queue<Node> queue = new LinkedList<>();
queue.offer(root);
while (queue.size()>0){
int size = queue.size();
List<Integer> level = new ArrayList<>();
for (;size>0;size--){
Node t = queue.poll();
Node left = t.left;
Node right = t.right;
if (left!=null){
queue.add(left);
}
if (right!=null){
queue.add(right);
}
level.add(t.value);
}
res.add(0,level);
}
return res;
}
public static List<List<Integer>> levelOrder(Node root){
if (root==null){
return new ArrayList<>();
}
List<List<Integer>> res = new ArrayList<>();
Queue<Node> queue = new LinkedList<>();
queue.offer(root);
while (queue.size()>0){
int size = queue.size();
List<Integer> level = new ArrayList<>();
for (;size>0;size--){
Node t = queue.poll();
Node left = t.left;
Node right = t.right;
if (left!=null){
queue.add(left);
}
if (right!=null){
queue.add(right);
}
level.add(t.value);
}
res.add(level);
}
return res;
}
public static List<Integer> simpleLevelOrder(Node root){
if (root==null){
return new ArrayList<>();
}
List<Integer> res = new ArrayList<>();
LinkedList<Node> queue = new LinkedList<>();
queue.add(root);
while (queue.size()>0){
int size = queue.size();
for (;size>0;size--){
Node t =queue.remove();
Node left =t.left;
Node right = t.right;
if (left!=null){
queue.add(left);
}
if (right!=null){
queue.add(right);
}
res.add(t.value);
}
}
return res;
}
}
class Node{
int value;
Node left;
Node right;
public Node() {
}
public Node(int value) {
this.value = value;
}
}
class NNode{
int value;
List<NNode> children;
public NNode(int value) {
this.value = value;
}
}