题目描述:

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]
]

题目解答:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
/**
     * 实质是二叉树的广度优先搜索
     * 利用一个辅助队列保存被访问的当前节点的左右孩子,
     * 调整进出队列的顺序以实现层序遍历
     */
    public List<List<Integer>> levelOrder(TreeNode root) {
         List<List<Integer>> result=new ArrayList<>();
          
         if(root==null){
            return result;
         }
         
        /**
         * 一般来说,队列的实现选择LinkedList即可
         * 队列保存节点就不用找什么变量了
         */
        Queue<TreeNode> queue=new LinkedList<>();
        queue.offer(root);
        //首先把头结点的值保存到结果集,然后把左右子节点分别进入队列
       while(!queue.isEmpty()){//需要使用isEmptyy判断,不能使用null
           List<Integer> temp=new ArrayList<>();
           /**
            * error!这里对进行循环的过程,队列长度是在不断变化的
            * size需要等于队列出队前的长度
            */
           int size =queue.size();
           for(int i=0;i<size;i++){
               TreeNode node=queue.poll();
               temp.add(node.val);
               if(node.left!=null)
                 queue.offer(node.left);
               if(node.right!=null)
                 queue.offer(node.right);
           }
           //当前队列全部poll,到这里已经完成了一层的遍历
           result.add(temp);
       }
        return result;
    }

  

本文转自邴越博客园博客,原文链接:http://www.cnblogs.com/binyue/p/4250267.html,如需转载请自行联系原作者

LeetCode之Binary Tree Level Order Traversal 层序遍历二叉树相关推荐

  1. 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 l ...

  2. LeetCode 107. Binary Tree Level Order Traversal II

    LeetCode 107. Binary Tree Level Order Traversal II Solution1:我的答案 比102那道题多了一行代码... /*** Definition f ...

  3. 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, ...

  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 rig ...

  5. LeetCode 102. Binary Tree Level Order Traversal

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

  6. Leetcode | 107. Binary Tree Level Order Traversal II

    题目:二叉树的层次遍历 II 1. 代码①:深度优先搜索(链接) /*** Definition for a binary tree node.* struct TreeNode {* int val ...

  7. 利用bds和dfs解决 LeetCode 107. Binary Tree Level Order Traversal II

    问题简述 给定一棵二叉树,返回该二叉树自底向上遍历的结点值(即从左到右,自底向上) 比如给定一颗二叉树 [3,9,20,null,null,15,7] 3/ \ 9 20/ \ 15 7 返回的结果为 ...

  8. 102. Binary Tree Level Order Traversal

    题目 Binary Tree Level Order Traversal 层次遍历二叉树 链接 Given a binary tree, return the level order traversa ...

  9. [LeetCode] Binary Tree Level Order Traversal 二叉树层次遍历(DFS | BFS)

    目录: 1.Binary Tree Level Order Traversal - 二叉树层次遍历 BFS 2.Binary Tree Level Order Traversal II - 二叉树层次 ...

最新文章

  1. Python 面向对象、封装
  2. Linux awk 命令 说明
  3. 598. Range Addition II(Python)
  4. 查看安装软件/Select-object/Where-Object xxx -like
  5. 面试题,你什么时候可以入职?回答不好,容易被压薪资
  6. ECSHOP邮件验证后送积分
  7. selenium的运行时异常
  8. 计算机一些常见名词解释
  9. python row column_将rowcolumnvalue数据转换为数组numpy
  10. python snap7 plc_Python-Snap7获取西门子PLC 300数值
  11. 火狐扩展插件开发者模式_使用插件扩展您的开发人员社区
  12. 阿里云服务器怎么用?阿里云服务器新手使用教程
  13. 金蝶云星空(Kingdee)的webapi 使用:修改生产领料单的实收数量,并且影响上下游单据(生产订单)
  14. Android-MMS中彩信附件的格式及分析
  15. 如何批量修改文件名?教你一招,轻松解决
  16. c语言调幅度程序,广播监测设备入网技术要求及测量方法(DOC 72页).doc
  17. 关于文献HEVC-The New Glod Standard For Video Compress的理解
  18. 闲话 | 人生,是一场怎样的修行
  19. 在win10安装pip
  20. 我所关注,推荐的公众号---软件那些事儿

热门文章

  1. Java日历打印_使用java 打印日历
  2. 活动报名小程序源码/thinkphp后台管理报名小程序源码
  3. 紫色管理系统UI bootstrap后台模板
  4. asp.net中的报销多级审批工作流 (状态机版本)
  5. PHP7革新与性能优化
  6. CSS3: 常用动画特效及4个最流行的动画库
  7. 在Ubuntu 上怎么连接装有iOS 7的iPhone或iPad
  8. UIKeyboard键盘相关知识点
  9. Linux——grep文本搜索命令
  10. LeetCode 26. Remove Duplicates from Sorted Array