码迷,mamicode.com
首页 > 编程语言 > 详细

leecode Binary Tree Level Order Traversal II java

时间:2014-07-01 13:30:53      阅读:300      评论:0      收藏:0      [点我收藏+]

标签:blog   http   java   for   io   leetcode   

做完这道题,只能说基本功很重要,数组中套数组就不会用了,过几天吧1做了,看自己到底等没。

https://oj.leetcode.com/problems/binary-tree-level-order-traversal-ii/

import java.util.*;
/**
 * Definition for binary tree
 * 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>> list=new ArrayList<ArrayList<Integer>>();
          ArrayList<ArrayList<Integer>> list = new ArrayList<ArrayList<Integer>>();  
          if(root==null) return (List)list;
         
          
       
          
    
        Queue<TreeNode> q=new LinkedList<TreeNode>();
        q.offer(root);
        while(!q.isEmpty())
        {
          ArrayList<Integer> temp=new ArrayList<Integer>();
          int len=q.size();
        
            for(int i=0;i<len;i++)
            {
                TreeNode t=q.poll();
                temp.add(t.val);
                if(t.left!=null) q.offer(t.left);
                
                if(t.right!=null) q.offer(t.right);
            }
            
           
            
             ArrayList<Integer> arry2=new ArrayList<Integer>(temp);
             list.add(arry2);
            temp.clear();
            
        }
        int n=list.size();
        
    for(int i=0;i<n/2;i++)
    {
        ArrayList temp=list.get(i);
        list.set(i,list.get(n-1-i));
        
        list.set(n-i-1,temp);
        //swap  
       
    }
        
   
        
        return (List)list;
    
    
        
    }
}

  

leecode Binary Tree Level Order Traversal II java,布布扣,bubuko.com

leecode Binary Tree Level Order Traversal II java

标签:blog   http   java   for   io   leetcode   

原文地址:http://www.cnblogs.com/hansongjiang/p/3817203.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!