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 返回其 ...
随机推荐
- 远程链接 aws Windows Server 2016 Base Nano
第一次接触Windows Server 2016 Base Nano,平时工作中或者自己私下使用win的服务器都是带桌面版本的 而这次在aws一不小心开了一台Nano服务,刚开始我都不知道这个是什么玩 ...
- 自用IP查询网址 - 地址 - 归属地 - 地理位置 - 2017.5
下面速度较快排行 http://city.ip138.com/ip2city.asp http://1212.ip138.com/ic.asp http://www.taobao.com/help/g ...
- 剑指Offer 26. 二叉搜索树与双向链表 (二叉搜索树)
题目描述 输入一棵二叉搜索树,将该二叉搜索树转换成一个排序的双向链表.要求不能创建任何新的结点,只能调整树中结点指针的指向. 题目地址 https://www.nowcoder.com/practic ...
- box-shadow做出一条线两种颜色
今天同事问我一个问题,说下图的效果是怎么实现的 我当时想都没有想说这不就是两条线嘛,他说是一条线用box-shadow做出来的,之前也没有遇到过,觉得很有意思就试了一把. 语法 box-shadow: ...
- 集群容器管理之swarm ---服务管理
服务管理 # 创建服务docker service create --replicas 1 --name hello busybox # docker service update --args &q ...
- 【leetcode】441. Arranging Coins
problem 441. Arranging Coins solution1: class Solution { public: int arrangeCoins(int n) { ; ; while ...
- mongo 使用find的返回值,转换为数组形式
$rs = $mongo->find(); var_dump(iterator_to_array($rs));
- C++ 是 编程界 的 背锅侠
C++ 是 编程界 的 背锅侠, C++ 背的包袱 之 庞大复杂, 举世瞩目, 令人感动 . C++ 标准 委员会 俨然 已成了一个 职业 . C++ 把 静态编译 体系 发展到 庞大复杂, C ...
- HTTP各种特性
一.Http客户端 1.浏览器.打开百度首页 2.Curl工具 二.CORS 跨域(浏览器的功能) 1.修改Server.js const http = require('http'); const ...
- zookeeper的WEB客户端zkui使用
转载自:http://blog.csdn.net/csolo/article/details/53694665 前面几篇实践说明了zookeeper如何配置和部署,如何开发,因为大多是后台操作,对于维 ...