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 返回其 ...
随机推荐
- Linux shell基础知识(上)
Linux shell基础知识(上) 目录 一.shell介绍 二.命令历史 三.命令补全和别名 四.通配符 五.输入输出重定向 六.管道符和作业控制 七.shell变量 八.环境变量配置文件 九.b ...
- 18-09-20 关于Excel 表格重复问题解决示例
代码实现:b=a.drop_duplicates(subset=None, keep='first', inplace=False) Excel 去除重复项 在测试过程中,会经常遇到多张表格筛选比对的 ...
- python自学第13天 hashlib,re模块
import hashlib sha=hashlib.sha3_512()#定义加密成什么格式 sha.update('how to use sha1 in 年后 '.encode('utf-8')) ...
- Debian下安装docker
1.安装docker.io包之前,需要先设置使用backports源 编辑/etc/apt/sources.list文件,加入下面这一句: deb http://http.debian.net/deb ...
- 公网定制化yum仓库部署
公网定制化yum仓库部署 (1)搭建公网源yum仓库 安装wget aliyun源 # wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun ...
- 深入理解Java中的synchronized锁重入
问题导入:如果一个线程调用了一个对象的同步方法,那么他还能不能在调用这个对象的另外一个同步方法呢? 这里就是synchronized锁重入问题. 一.synchronized锁重入 来看下面的代码: ...
- s21day07 python笔记
s21day07 python笔记 一.昨日内容回顾及补充 回顾 补充 将前面所提到的功能,统一改称为方法 二.深浅拷贝 基本格式 v1 = [1,2,3] import copy v2 = copy ...
- Js高级 事件冒泡
什么叫事件冒泡 当给父子元素的同一事件绑定方法时,触发了子元素身上的事件,执行完毕之后,也会触发父级元素的相同事件,这种传播机制叫事件冒泡. 取消事件冒泡 Event对象有个属性叫cancelBubb ...
- 2018.4.28 kvm虚拟机管理
创建虚拟机: virt-install --name wj-v1.4.1.0411 --vcpus=2 --memory=2048 --disk path=/home/wj/os/osgwV1.4.1 ...
- PythonStudy——字符串重要方法 String important method
# 1.索引(目标字符串的索引位置) s1 = '123abc呵呵' print(s1.index('b')) # 2.去留白(默认去两端留白,也可以去指定字符) s2 = '***好 * 的 *** ...