题目:

LeetCode Premium Subscription
Problems
Pick One
Mock
Articles
Discuss
Book
fengsehng
102. Binary Tree Level Order Traversal My Submissions QuestionEditorial Solution
Total Accepted: 98313 Total Submissions: 302608 Difficulty: Easy
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 order traversal as:
[
  [3],
  [9,20],
  [15,7]
]

题意:

  • 题意是把一颗二叉树,按照从上到下,,把每一排节点从左到右存进list,最后把所有list存进一个list
  • 考虑用递归,设置两个Queue,first和second来存放TreeNode。first来存放当前最下层的一行,second用来存放下一行,first的元素的左右节点赋值给second,把second的元素给first,往下移动
  • 考虑first是空的时候,停止,注意判断临时数组tmp是否为空,非空才能存进
  • -

代码:

/**
 * 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>> levelOrder(TreeNode root) {
        List<List<Integer>> all = new ArrayList<List<Integer>>();
        Queue<TreeNode> first = new LinkedList<TreeNode>();
        Queue<TreeNode> second = new LinkedList<TreeNode>();
        if(root == null){
            return all;
        }
        List<Integer> tmp = new ArrayList<Integer>();
        tmp.add(root.val);
        all.add(tmp);
        first.add(root);
        while(!first.isEmpty()){
            TreeNode node = first.poll();
            if(node.left != null){
                second.add(node.left);
            }
            if(node.right != null){
                second.add(node.right);
            }
            if(first.isEmpty()){
                List<Integer> tmp1 = new ArrayList<Integer>();
                while(!second.isEmpty()){
                    TreeNode n = second.poll();
                    first.add(n);
                    tmp1.add(n.val);
                }
                if(tmp1.size() != 0){
                    all.add(tmp1);
                }
                second.clear();
            }

        }
        return all;
    }
}

LeetCode(32)-Binary Tree Level Order Traversal的更多相关文章

  1. Java for LeetCode 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 ...

  2. LeetCode 107 Binary Tree Level Order Traversal II(二叉树的层级顺序遍历2)(*)

    翻译 给定一个二叉树,返回从下往上遍历经过的每一个节点的值. 从左往右,从叶子到节点. 比如: 给定的二叉树是 {3,9,20,#,#,15,7}, 3 / \ 9 20 / \ 15 7 返回它从下 ...

  3. [LeetCode] 107. Binary Tree Level Order Traversal II 二叉树层序遍历 II

    Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left ...

  4. [LeetCode] 102. Binary Tree Level Order Traversal 二叉树层序遍历

    Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, ...

  5. 【leetcode】Binary Tree Level Order Traversal I & II

    Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, ...

  6. LeetCode之Binary Tree Level Order Traversal 层序遍历二叉树

    Binary Tree Level Order Traversal 题目描述: Given a binary tree, return the level order traversal of its ...

  7. (二叉树 BFS) leetcode 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 ...

  8. LeetCode 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 ...

  9. [Leetcode][JAVA] Binary Tree Level Order Traversal

    Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, ...

随机推荐

  1. EBS应收(AR)模块常用表

     select * from ar_batches_all 事务处理批 select * from ra_customer_trx_all INVOICE头 select * from ra_cu ...

  2. 【一天一道Leetcode】#190.Reverse Bits

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 我的个人博客已创建,欢迎大家持续关注! 一天一道le ...

  3. 07 总结ProgressDialog 异步任务

    1,ProgressDialog     >        //使用对象  设置标题             progressDialog.setTitle("标题");   ...

  4. C++_友元函数

    1.为什么要引入友元函数:在实现类之间数据共享时,减少系统开销,提高效率       具体来说:为了使其他类的成员函数直接访问该类的私有变量       即:允许外面的类或函数去访问类的私有变量和保护 ...

  5. 从浏览器直接转跳到APP具体页面---(魔窗)MagicWindow使用教程

    想要实现在网页里一键调到你APP的指定页面吗,好比打开 JD的一个商品的网页,从网页调到APP这个商品的页面.APP服务化, 使用魔窗SDK可以轻松实现! 老规矩:效果图奉上 1.注册魔窗账号,创建A ...

  6. jquery 只读

    大家都理解这是什么,正常的写法如下: if (status == true) { $("#minDelistStr").val(totalAmount);// 去掉首部的" ...

  7. 怎么在Eclipse中添加VI插件

    下载地址 Vi插件下载位置 怎么安装? 将下载下来的zip文件进行解压,然后把对于的目录下的文件分别复制到eclipse目录下的plugins 和features目录下: 注册 在eclipse根目录 ...

  8. C#之FileInfo的简单操作

    和DirectoryInfo一样,FileInfo类同样是很方便的就能实现复杂的功能. 如下,我们一起来看一下一个简单的小例子吧. using System; using System.Collect ...

  9. android:layout_alignleft layout_toleftof区别,详解RelativeLayout布局属性

    转载请注明博客地址. 最近看博客看到有关于RelativeLayout布局的解释,有的解释很多是错误的.因此有必要对每一个常见的布局属性进行描述.以下解释全部都是逐行进行测试的. 首先把常用的布局分组 ...

  10. Chipmunk Rigid Bodies:cpBody

    Chipmunk刚体支持3种不同的类型: Dynamic(动态),Static(静态)以及Kinematic(混合态)刚体.它们拥有不同的行为和性能特征. 动态刚体是默认的刚体类型.它们可以对碰撞做出 ...