leetcode102
本题是广度优先遍历(BFS)实现树的层次遍历,使用队列实现。
class Solution {
public:
vector<vector<int>> levelOrder(TreeNode* root) {
vector<vector<int>> res;
queue<TreeNode*>q;
if (root != NULL)
{
q.push(root);
while (!q.empty())
{
vector<int> tmp;
vector<TreeNode*> T;
while (!q.empty())
{
TreeNode* t = q.front();
q.pop();
tmp.push_back(t->val);
if (t->left != NULL)
{
T.push_back(t->left);
}
if (t->right != NULL)
{
T.push_back(t->right);
}
}
res.push_back(tmp);
for (auto x : T)
{
q.push(x);
}
}
}
return res;
}
};
补充一个python的实现:
class Solution:
def lOrder(self,temp,result):
count = len(temp)
newary = []
while count > :
top = temp.pop()
newary.append(top.val)
count -=
if top.left != None:
temp.append(top.left)
if top.right != None:
temp.append(top.right)
if len(newary) > :
result.append(newary)
if len(temp) > :
self.lOrder(temp,result) def levelOrder(self, root: TreeNode) -> List[List[int]]:
result = []
temp = []
if root != None:
temp.append(root)
self.lOrder(temp,result)
return result
leetcode102的更多相关文章
- 剑指offer从上往下打印二叉树 、leetcode102. Binary Tree Level Order Traversal(即剑指把二叉树打印成多行、层序打印)、107. Binary Tree Level Order Traversal II 、103. Binary Tree Zigzag Level Order Traversal(剑指之字型打印)
从上往下打印二叉树这个是不分行的,用一个队列就可以实现 class Solution { public: vector<int> PrintFromTopToBottom(TreeNode ...
- LeetCode102 Binary Tree Level Order Traversal Java
题目: Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to ri ...
- [Swift]LeetCode102. 二叉树的层次遍历 | Binary Tree Level Order Traversal
Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, ...
- (二叉树 BFS) leetcode102. 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,107,103】 二叉树的层次遍历
102. 二叉树的层次遍历 (1过,隐蔽错误花时间很多,简单题目本应很快,下次注意红色错误的地方) 给定一个二叉树,返回其按层次遍历的节点值. (即逐层地,从左到右访问所有节点). 例如:给定二叉树: ...
- 32-2题:LeetCode102. Binary Tree Level Order Traversal二叉树层次遍历/分行从上到下打印二叉树
题目 给定一个二叉树,返回其按层次遍历的节点值. (即逐层地,从左到右访问所有节点). 例如: 给定二叉树: [3,9,20,null,null,15,7], 3 / \ 9 20 / \ 15 7 ...
- leetcode-102.层序遍历二叉树(正序)· BTree
题面 Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to rig ...
- LeetCode102. 二叉树的层次遍历
102. 二叉树的层次遍历 描述 给定一个二叉树,返回其按层次遍历的节点值. (即逐层地,从左到右访问所有节点). 示例 例如,给定二叉树: [3,9,20,null,null,15,7], 3 / ...
- Leetcode102. Binary Tree Level Order Traversal二叉树的层次遍历
给定一个二叉树,返回其按层次遍历的节点值. (即逐层地,从左到右访问所有节点). 例如: 给定二叉树: [3,9,20,null,null,15,7], 3 / \ 9 20 / \ 15 7 返回其 ...
随机推荐
- kafka 分区和副本以及kafaka 执行流程,以及消息的高可用
1.Kafka概览 Apache下的项目Kafka(卡夫卡)是一个分布式流处理平台,它的流行是因为卡夫卡系统的设计和操作简单,能充分利用磁盘的顺序读写特性.kafka每秒钟能有百万条消息的吞吐量,因此 ...
- python day19--面向对象,接口,封装
#1.接口类,对象类.规范类中方法的统一. # 第一版:三个类,每个类中有相同的方法 # class Alipay: # def __init__(self,money): # self.money= ...
- ecmall 学习记录
1. /* 载入配置项 */ $setting =& af(MODULE); Conf::load($setting->getAll()); af方法定义如下: /** * 获取数组文件 ...
- 2018年3月底的PTA(二)
C高级第二次PTA作业(1) 题目6-7 删除字符串中数字字符 1.设计思路 为了偷懒,本题算法和流程图是精简代码后的,具体请看本题实验代码的第二段代码. (1)算法(子函数) 第一步:定义子函数类型 ...
- php之强制回调类型callable
<?php function demo(callable $fn) { $fn(); } function callback() { echo __FUNCTION__,'<br/> ...
- h5 js判断是安卓还是ios设备,跳转到对应的下载地址
/*ios和安卓跳转 js*/$(function(){ var u = navigator.userAgent; var ua = navigator.userAgent.toLowerCase() ...
- PTA寒假三
抓老鼠啊~亏了还是赚了? (20 分) 某地老鼠成灾,现悬赏抓老鼠,每抓到一只奖励10元,于是开始跟老鼠斗智斗勇:每天在墙角可选择以下三个操作:放置一个带有一块奶酪的捕鼠夹(T),或者放置一块奶酪(C ...
- vc6.0使用
1.文件结构 工作空间dsw 工程1 Source file .cpp,main Header file .h Resource files 工程2 ...
- #考研笔记#计算机之PPT问题
PPT1. 幻灯片母版.版式.模板之间的联系与区别?见下 12 2. 占位符的意义用于幻灯片上,就表现为一个虚框,虚框内部往往有“单击此处添加标题”之类的提示语,一旦鼠标点击之后,提示语会自动消失.当 ...
- SQLServer调WebService & 错误解决:请求格式无法识别
(sqlServer 2008 + VS2010) 首先,对服务器进行配置. sp_configure ; GO RECONFIGURE; GO sp_configure ; GO RECONFIGU ...