LintCode 二叉树的层次遍历 II题目代码

时间:2022-06-07
本文章向大家介绍LintCode 二叉树的层次遍历 II题目代码,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

题目

给出一棵二叉树,返回其节点值从底向上的层次序遍历(按从叶节点所在层到根节点所在的层遍历,然后逐层从左往右遍历)

样例 给出一棵二叉树 {3,9,20,#,#,15,7},

Paste_Image.png

代码

/**
 * Definition of TreeNode:
 * public class TreeNode {
 *     public int val;
 *     public TreeNode left, right;
 *     public TreeNode(int val) {
 *         this.val = val;
 *         this.left = this.right = null;
 *     }
 * }
 */
 
 
public class Solution {
    /**
     * @param root: The root of binary tree.
     * @return: buttom-up level order a list of lists of integer
     */
    public ArrayList<ArrayList<Integer>> levelOrderBottom(TreeNode root) {
        // write your code here
        if(root == null)
            return new ArrayList<>();
        
        ArrayList<ArrayList<Integer>> res = new ArrayList<>();
        
        Queue<TreeNode> list = new LinkedList<>();
        
        list.offer(root);
        
        while(!list.isEmpty()) {
            int size = list.size();
            ArrayList<Integer> level = new ArrayList<>();
            for(int i=0;i<size;i++) {
                TreeNode cur = list.poll();
                level.add(cur.val);
                if(cur.left != null)
                    list.offer(cur.left);
                if(cur.right != null)
                    list.offer(cur.right);
            }
            res.add(level);
        }
        Collections.reverse(res);
        return res;
    }
}