leecode Binary Tree Level Order Traversal II java

做完这道题,只能说基本功很重要,数组中套数组就不会用了,过几天吧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

上一篇:大整数乘法 python实现


下一篇:python encode和decode函数说明【转载】