Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left to right, level by level from leaf to root).

For example:
Given binary tree {3,9,20,#,#,15,7},

    3
   / \
  9  20
    /  \
   15   7


return its bottom-up level order traversal as:

[
  [15,7],
  [9,20],
  [3]
]


思路,采用广度优先搜索的策略,使用队列作为数据结构,当一层节点压入队列后,依次取出将该层节点的下一层节点全部压入队列中,再依次执行操作,层与层之间,使用null节点分割


另外一个问题是,我们处理数据是自顶向下,然而需要的输出结果是自底向上的,因此我定义了另外一个list,将结果list反向倒入目标list中

/**

 * Definition for a binary tree node.

 * public class TreeNode {

 *     int val;

 *     TreeNode left;

 *     TreeNode right;

 *     TreeNode(int x) { val = x; }

 * }

 */

public class Solution {

    

    int count(TreeNode x)

    {

        if(x==null)

            return 0;

        else 

        {

            int lc=count(x.left);

            int rc=count(x.right);

            return lc+rc+1;

        }

    }

    

    int height(TreeNode x)

    {

        if(x==null)

            return 0;

        else 

        {

            int lh=height(x.left);

            int rh=height(x.right);

            if(lh>rh)

                return lh+1;

            else

                return rh+1;

        }

    }

    

    TreeNode[] queue;

    int start=0;

    int end=0;

    int size=0;

    

    void enqueue(TreeNode x)

    {

        queue[start]=x;

        //if(isEmpty())

        //    end=(end+1)%size;

        start=(start+1)%size;

        

    }

    

    TreeNode outqueue()

    {

        //start=(start-1)%size;

        end=(end+1)%size;

        return queue[(end-1)%size];

    }

    

    boolean isEmpty()

    {

        if(end==start)

            return true;

        else

            return false;

    }

    

    public List<List<Integer>> levelOrderBottom(TreeNode root) {

        

        List<List<Integer>> listl=new ArrayList<List<Integer>>();

        List<List<Integer>> listend=new ArrayList<List<Integer>>();

        size=(count(root)+1)*10;

        int H=height(root);

        

        //List<Integer>[] v;

        //for(int i=0;i<H;i++)

        //    v[i]=new ArrayList<Integer>();

        

        

        queue=new TreeNode[size];

        for(int i=0;i<size;i++)

            queue[i]=new TreeNode(0);

        

        TreeNode y=null;

 

        enqueue(root);

        enqueue(y);

        int c=0;

        

        while(!isEmpty())

        {

            boolean t=false;

            List<Integer> l=new ArrayList<Integer>();

            TreeNode z=outqueue();

            while(z!=null)

            {

                l.add(z.val);

                if(z.left!=null)

                {

                    enqueue(z.left);

                    t=true;

                }

                if(z.right!=null)

                {

                    enqueue(z.right);

                    t=true;

                }

                z=outqueue();

            }

            if(!l.isEmpty())

                listl.add(l);

            if(t)

            {

                enqueue(y);

                c++;

            }

        }

        //以下是反向输出结果,使输出结果从自顶向下转变为自底向上

        for(int j=listl.size()-1;j>=0;j--)

            listend.add(listl.get(j));

        return listend;

    }

}