LintCode 二叉树的层次遍历 II

题目

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

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

LintCode 二叉树的层次遍历 II_第1张图片
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> levelOrderBottom(TreeNode root) {
        // write your code here
        if(root == null)
            return new ArrayList<>();
        
        ArrayList> res = new ArrayList<>();
        
        Queue list = new LinkedList<>();
        
        list.offer(root);
        
        while(!list.isEmpty()) {
            int size = list.size();
            ArrayList level = new ArrayList<>();
            for(int i=0;i

你可能感兴趣的:(LintCode 二叉树的层次遍历 II)