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 order traversal as:

[
[3],
[9,20],
[15,7]
]

题目解答:

/**
* 实质是二叉树的广度优先搜索
* 利用一个辅助队列保存被访问的当前节点的左右孩子,
* 调整进出队列的顺序以实现层序遍历
*/
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;
}

  

LeetCode之Binary Tree Level Order Traversal 层序遍历二叉树的更多相关文章

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

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

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

  3. 102. Binary Tree Level Order Traversal ------层序遍历

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

  4. [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 ...

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

  6. [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, ...

  7. LeetCode(32)-Binary Tree Level Order Traversal

    题目: LeetCode Premium Subscription Problems Pick One Mock Articles Discuss Book fengsehng 102. Binary ...

  8. (二叉树 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 ...

  9. 【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, ...

随机推荐

  1. JQ库函数记忆要点

    1.核心(1.核心函数2.对象访问3.数据缓存4.队列控制4.插件机制5.多库共存) 2.属性(1.属性2.css类3.HTML代码/文本/值) 3.选择器(表单,表单对象属性,基本,内容,子元素,层 ...

  2. Struts2 Action 动态传参数

    Struts2的两个Action之间传参的问题. 需求功能是这样:Action1 获取数据库配置内容,得到相应Model的 动态URL ,这里的URL 有的是Action有的是JSP页面. 1.使用r ...

  3. 浅谈python的import

    动态加载模块: 使用__import__ c=__import__('sys') 等价于 import sys 不过前者可以在执行时决定. 静态加载: 也就是常规的import from xxx im ...

  4. ndk学习19: 使用Eclipse调试so

    1.  设置调试选项 在AndroidManifest文件加入允许调试 android:debuggable="true"   此时编译项目会多出: 2.  配置调试代码 把需要调 ...

  5. InstallUtil在windows服务中的使用(转)+ 服务安装的注意事项

    1.  新建一个Windows Service的方法: 1. 打开Visual Studio 2008新建一个project Solution: 2. 选择Windows->windows Se ...

  6. SqlServer coalesce函数

    SqlServer数据库中coalesce函数用法:在SqlServer2005中有了新的函数,它非常的实用,它就是coalesce函数,此函数可以返回参数中的第一个非空表达式,当你要在N个字段中选取 ...

  7. Best Meeting Point

    Total Accepted: 701 Total Submissions: 1714 Difficulty: Medium A group of two or more people wants t ...

  8. CentOS7下安装soaplib

    测试安装 cd ENV . bin/activate yum install libxml2-devel libxslt-devel pip install soaplib 安装中遇到的问题: Uni ...

  9. nyoj412_bitset_

    Same binary weight 时间限制:300 ms  |  内存限制:65535 KB 难度:3   描述 The binary weight of a positive  integer ...

  10. nyoj123_士兵杀敌(四)_树状数组_插线求点

    士兵杀敌(四) 时间限制:2000 ms  |  内存限制:65535 KB 难度:5   描述 南将军麾下有百万精兵,现已知共有M个士兵,编号为1~M,每次有任务的时候,总会有一批编号连在一起人请战 ...