102. 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,null,null,15,7]
,
3
/ \
9 20
/ \
15 7
return its level order traversal as:
[
[3],
[9,20],
[15,7]
] 层序遍历 利用queue
class Solution {
public List<List<Integer>> levelOrder(TreeNode root) {
List<List<Integer>> res = new ArrayList<List<Integer>>();
Queue<TreeNode> queue = new LinkedList<TreeNode>();
if(root==null) return res;
queue.add(root);
while (!queue.isEmpty()) {
int levlnum = queue.size();
List<Integer> res_temp = new ArrayList<Integer>();
for (int i = 0;i<levlnum ;i++ ){ //把每层的左右节点都保存到queue里
//并讲当层的值从queue里弹出,加到res_temp 中
if(queue.peek().left!=null) queue.add(queue.peek().left);
if(queue.peek().right!=null) queue.add(queue.peek().right); res_temp.add(queue.poll().val);
}
res.add(res_temp);
}
return res;
}
}
102. Binary Tree Level Order Traversal ------层序遍历的更多相关文章
- LeetCode之Binary Tree Level Order Traversal 层序遍历二叉树
Binary Tree Level Order Traversal 题目描述: Given a binary tree, return the level order traversal of its ...
- 102. Binary Tree Level Order Traversal 广度优先遍历
Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, ...
- [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, ...
- [刷题] 102 Binary Tree Level Order Traversal
要求 对二叉树进行层序遍历 实现 返回结果为双重向量,对应树的每层元素 队列的每个元素是一个pair对,存树节点和其所在的层信息 1 Definition for a binary tree node ...
- 【LeetCode】102. Binary Tree Level Order Traversal (2 solutions)
Binary Tree Level Order Traversal Given a binary tree, return the level order traversal of its nodes ...
- 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, ...
- 【LeetCode】102. Binary Tree Level Order Traversal 二叉树的层序遍历 (Python&C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 BFS DFS 日期 题目地址:https://lee ...
- LeetCode 102. Binary Tree Level Order Traversal 二叉树的层次遍历 C++
Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, ...
- 【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, ...
随机推荐
- nodejs操作图片方法
最近项目中用到nodejs做图片服务器,用户上传图片生成缩略图返回地址一系列操作. 原来一直用.net平台,所有都封装好了生成缩略图这种分分钟就解决了,遂度娘一番全是调用imagemagick和gra ...
- win10执行shell脚本
我们在win10如何执行以.sh文件的脚本呢? 开发步骤:1.写脚本b2q_goods.sh #!/bin/bashsql="select * from b2q.goods where go ...
- Grodno 2015 (Urozero May 2015 Day 5) D Triangles
给出$P(<=10^9)$, 求有多少个有序三元组$(a, b, c),\ gcd(a, b, c) = 1,\ a + b + c <= P$且以它们构成的三角形中存在某个角是另外一个角 ...
- kafka2:常用topic命令
常用操作 创建一个topic bin/kafka-topics.sh --create --zookeeper 192.168.3.230:2181 --replication-factor 3 -- ...
- push certificate
developer_identity.cer <= download from Applemykey.p12 <= Your private key openssl x509 -in de ...
- sass学习之一:sass安装compass部署
主要参考 http://www.jianshu.com/p/5bfc9411f58f -------------------------------------------- sass基于ruby 需 ...
- Python全栈day26-27(面向对象进阶)
参考 http://www.cnblogs.com/linhaifeng/articles/6204014.html 1,什么是反射 反射的概念是由Smith在1982年首次提出的,主要是指程序可以访 ...
- phpmail发送邮件
---恢复内容开始--- 首先.需要phpmailer的包. 地址:https://github.com/Synchro/PHPMailer 解开压缩包,将class.phpmailer.php,cl ...
- 三报文握手而不是三次握手 wireshark 封包详细信息 (Packet Details Pane) wireshark与对应的OSI七层模型 TCP包的具体内容 分析TCP三次握手过程
总结: 1.tcp报文非数据部分4*6字节 2.RFC 973 <计算机网络> 谢希仁 three way (three message) handshake 只是一次握手 同步位SYN. ...
- 【python】-- 类的创建、__new__、__metaclass___
类的创建 前面的随笔都是关于类的知识,通过类创建对象,那这个类到底是怎么产生的呢? 1. 传统创建类 class Foo(object): def __init__(self,name): self. ...