思路:
// @Title: 彩灯装饰记录 III (彩灯装饰记录 III)
// @Author: qisiii
// @Date: 2022-02-21 12:51:19
// @Runtime: 3 ms
// @Memory: 41.4 MB
// @comment:
// @flag:
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public static List<List<Integer>> levelOrder(TreeNode root) {
LinkedBlockingQueue queue = new LinkedBlockingQueue();
List<List<Integer>> result = new ArrayList<>();
if (root==null){
return result;
}
queue.offer(root);
doTree(queue, result,false);
return result;
}
public static void doTree(Queue<TreeNode> queue, List list,boolean zhi) {
if (queue==null||queue.isEmpty()){
return;
}
List<Integer> objects = new ArrayList<>();
Queue<TreeNode> temp = new LinkedBlockingQueue();;
while (!queue.isEmpty()) {
TreeNode poll = queue.remove();
if (poll==null){
continue;
}
if(zhi){
objects.add(0,poll.val);
}else{
objects.add(poll.val);
}
if (poll.left!=null){
temp.offer(poll.left);
}
if (poll.right!=null){
temp.offer(poll.right);
}
}
if (!objects.isEmpty()) {
list.add(objects);
}
doTree(temp, list,!zhi);
}
}