package com.example.demo;
import org.springframework.util.CollectionUtils;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;
/**
* 102. 二叉树的层序遍历
* 给你一个二叉树,请你返回其按 层序遍历 得到的节点值。 (即逐层地,从左到右访问所有节点)。
* <p>
* <p>
* <p>
* 示例:
* 二叉树:[3,9,20,null,null,15,7],
* <p>
* 3
* /
* 9 20
* /
* 15 7
* 返回其层次遍历结果:
* <p>
* [
* [3],
* [9,20],
* [15,7]
* ]
* 通过次数144,064提交次数229,041
*/
public class Lc102 {
public static void main(String[] args) {
Integer[] arr = new Integer[]{1, 2, 3, 4, 5, 6, 7};
TreeNode root = CreateNode.createTree(arr).get(0);
List<List<Integer>> lists = levelOrder(root);
lists.forEach(n -> {
n.forEach(m -> {
System.out.print(m + ",");
});
System.out.println();
});
}
public static List<List<Integer>> levelOrder(TreeNode root) {
List<List<Integer>> res = new ArrayList<>();
if (root == null) {
return res;
}
Queue<TreeNode> queue = new LinkedList<>();
TreeNode curr = root;
queue.add(curr);
while (!queue.isEmpty()) {
List<Integer> list = new ArrayList<>();
int size = queue.size();
for (int i = 0; i < size; i++) {
list.add(queue.peek().val);
curr = queue.poll();
if (curr.left != null) {
queue.add(curr.left);
}
if (curr.right != null) {
queue.add(curr.right);
}
}
if (!CollectionUtils.isEmpty(list)) {
res.add(list);
}
}
return res;
}
}