本题是广度优先遍历(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的更多相关文章

  1. 剑指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 ...

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

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

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

  5. 【leetcode-102,107,103】 二叉树的层次遍历

    102. 二叉树的层次遍历 (1过,隐蔽错误花时间很多,简单题目本应很快,下次注意红色错误的地方) 给定一个二叉树,返回其按层次遍历的节点值. (即逐层地,从左到右访问所有节点). 例如:给定二叉树: ...

  6. 32-2题:LeetCode102. Binary Tree Level Order Traversal二叉树层次遍历/分行从上到下打印二叉树

    题目 给定一个二叉树,返回其按层次遍历的节点值. (即逐层地,从左到右访问所有节点). 例如: 给定二叉树: [3,9,20,null,null,15,7], 3 / \ 9 20 / \ 15 7 ...

  7. leetcode-102.层序遍历二叉树(正序)· BTree

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

  8. LeetCode102. 二叉树的层次遍历

    102. 二叉树的层次遍历 描述 给定一个二叉树,返回其按层次遍历的节点值. (即逐层地,从左到右访问所有节点). 示例 例如,给定二叉树: [3,9,20,null,null,15,7], 3 / ...

  9. Leetcode102. Binary Tree Level Order Traversal二叉树的层次遍历

    给定一个二叉树,返回其按层次遍历的节点值. (即逐层地,从左到右访问所有节点). 例如: 给定二叉树: [3,9,20,null,null,15,7], 3 / \ 9 20 / \ 15 7 返回其 ...

随机推荐

  1. 2018 ICPC 区域赛 焦作场 D. Keiichi Tsuchiya the Drift King(计算几何)

    http://codeforces.com/gym/102028/problem/D 题意:根据题中给的那个图,然后题目给你 a,b,r,d,让你求出最小的满足矩形通过弯道的w 思路:

  2. Unity发布WebGl注意事项

    unity 版本是5.5,不过看了2017的文档好像也是差不多,绝大部分都是根据官方文档,希望有帮助,如果有错误或者你知道更多这方面的只是,请告知下,大恩言谢. 1:对webgl发布的工程文件说明   ...

  3. componentWillMount和componentDidMount的区别

    1.componentWillMount  将要装载,在render之前调用: componentDidMount,(装载完成),在render之后调用 2.componentWillMount  每 ...

  4. junit断言和junit注释assert

    JUnit - 使用断言 断言 所有的断言都包含在 Assert 类中 public class Assert extends java.lang.Object 这个类提供了很多有用的断言方法来编写测 ...

  5. Flink实战(1) - Apache Flink安装和示例程序的执行

    在Windows上安装 从官方网站下载需要的二进制包 比如我下载的是flink-1.2.0-bin-hadoop2-scala_2.10.tgz,解压后进入bin目录 可以执行bat文件,也可以使用c ...

  6. hdu4612 Warm up 缩点+树的直径

    题意抽象后为:给定一个无向图 问添加一条边的情况下最少能有多少个桥. 桥的定义:删除该边后原图变为多个连通块. 数据规模:点数N(2<=N<=200000),边数M(1<=M< ...

  7. CCF-20170903-JSON查询

    这道题当时考ccf,五道题中做的时间最长的一道题....可惜最好只有0分!! 后来重现写了一下--(110行超级麻烦 主要思想:就是先对括号经行匹配,创建的时候分为创建表和创建元素两种情况,难点在于对 ...

  8. 2018.4.27 python使用过的第三方库

    Flask flask-login flask-sqlalchemy flask-mail psutil lvm2py oss2 python-ldap pyudev pyOpenSSL urllib ...

  9. 工作中 sql 整理(一)

    这篇文章记录关于SQL的内容,有些凌乱,是工作中点滴的积累,只能按照时间顺序,逐次记录. 一.update 关联更新 1.需求 Table A   TableB A表中的主键和B表中的主键相关联,关联 ...

  10. zombodb 配置设置

    主要是关于es 集群地址以及分片,复制副本的配置,配置主要在postgresql.conf,当然我们可以在函数中指定 postgresql.conf 级别的配置 es 配置 格式 zdb.defaul ...