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

时间: 2024-10-12 12:33:21

leecode Binary Tree Level Order Traversal II java的相关文章

leetcode 107 Binary Tree Level Order Traversal II ----- java

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,null,null,15,7], 3 / 9 20 / 15 7 return its bottom-up level order

107. Binary Tree Level Order Traversal II Java Solutions

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 trave

Binary Tree Level Order Traversal II leetcode java

题目: 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 t

LeetCode_107. Binary Tree Level Order Traversal II

107. Binary Tree Level Order Traversal II Easy 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,null,null,15,7], 3

[LeetCode]Binary Tree Level Order Traversal II

Binary Tree Level Order Traversal II 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 re

33: Binary Tree Level Order Traversal II

/************************************************************************/        /*       33:      Binary Tree Level Order Traversal II                                         */        /**************************************************************

leetCode 107. Binary Tree Level Order Traversal II 二叉树层次遍历反转

107. Binary Tree Level Order Traversal II 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,null,null,15,7],     3  

LeetCode之“树”:Binary Tree Level Order Traversal &amp;&amp; Binary Tree Level Order Traversal II

Binary Tree Level Order Traversal 题目链接 题目要求: Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, level by level). For example: Given binary tree {3,9,20,#,#,15,7}, 3 / 9 20 / 15 7 return its level orde

[leetcode]Binary Tree Level Order Traversal II @ Python

原题地址:http://oj.leetcode.com/problems/binary-tree-level-order-traversal-ii/ 题意: 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