package me.wayne.leetcodetop100.binarytree;

import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

import me.wayne.leetcodetop100.TreeNode;

/**
 * 给你二叉树的根节点 root ，返回其节点值 自底向上的层序遍历 。
 * （即按从叶子节点所在层到根节点所在的层，逐层从左向右遍历）
 * <p>
 * https://leetcode-cn.com/problems/binary-tree-level-order-traversal-ii/
 */
public class LC107_LevelOrderBottom {

    public List<List<Integer>> levelOrderBottom(TreeNode root) {
        if (root == null) {
            return Collections.emptyList();
        }

        List<List<Integer>> res = new LinkedList<>();
        Queue<TreeNode> queue = new LinkedList<>();
        queue.offer(root);
        while (!queue.isEmpty()) {
            List<TreeNode> nodes = new ArrayList<>();
            while (!queue.isEmpty()) {
                TreeNode node = queue.poll();
                nodes.add(node);
            }

            List<Integer> result = new ArrayList<>();
            for (TreeNode node: nodes) {
                result.add(node.val);
                if (node.left != null) {
                    queue.offer(node.left);
                }
                if (node.right != null) {
                    queue.offer(node.right);
                }
            }
            res.add(0, result);
        }
        return res;
    }
}

@@ -371,16 +371,59 @@
 sal-ii/%0A
+ * %E6%80%9D%E8%B7%AF%EF%BC%9A%E5%B1%82%E6%AC%A1%E9%81%8D%E5%8E%86%E7%9A%84%E6%97%B6%E5%80%99%EF%BC%8C%E6%AF%8F%E6%AC%A1%E6%B7%BB%E5%8A%A0%E7%BB%93%E6%9E%9CList%3CInteger%3E%E5%88%97%E8%A1%A8%E9%83%BD%E6%B7%BB%E5%8A%A0%E5%88%B0%E7%AC%AC0%E4%B8%AA%0A
  */%0Apubl

add comments in LC107_LevelOrderBottom