• 107. Binary Tree Level Order Traversal II


    /**
     * Definition for a binary tree node.
     * public class TreeNode {
     *     int val;
     *     TreeNode left;
     *     TreeNode right;
     *     TreeNode(int x) { val = x; }
     * }
     */
    public class Solution {
        public List<List<Integer>> levelOrderBottom(TreeNode root) {
            List<List<Integer>> res=new ArrayList<List<Integer>>();
            if(root==null)
                return res;
            Queue<TreeNode> q=new LinkedList<TreeNode>();
            q.offer(root);
            int size=q.size();
            int count=0;
            List<Integer> templist=new ArrayList<Integer>();
            while(!q.isEmpty())
            {
                TreeNode temp=q.poll();
                if(temp.left!=null)
                    q.offer(temp.left);
                if(temp.right!=null)
                    q.offer(temp.right);
                count++;
                templist.add(temp.val);
                if(count==size)
                {
                    size=q.size();
                    count=0;
                    res.add(templist);
                    templist=new ArrayList<Integer>();
                }
            }
            List<List<Integer>> revres=new ArrayList<List<Integer>>();
            for(int i=res.size()-1;i>=0;i--)
                revres.add(res.get(i));
            return revres;        
            
        }
    }
  • 相关阅读:
    oracle插入数据
    保存图片
    ASCII码排序及md5加密
    JavaScript
    HTML
    py访问Redis和zk操作
    Zookeeper集群搭建以及python操作zk
    并发编程
    Python之socket(套接字)
    Python 网络编程
  • 原文地址:https://www.cnblogs.com/aguai1992/p/5348189.html
Copyright © 2020-2023  润新知