【Lintcode】070.Binary Tree Level Order Traversal II
题目:
Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left to right, level by level from leaf to root).
Example
Given binary tree {3,9,20,#,#,15,7},
3
/ \
9 20
/ \
15 7
return its bottom-up level order traversal as:
[
[15,7],
[9,20],
[3]
]
题解:
迭代法,利用队列,不能用栈。
Solution 1 ()
class Solution {
public:
vector<vector<int>> levelOrderBottom(TreeNode *root) {
vector<vector<int>> res;
if (root == nullptr) {
return res;
}
vector<int> v;
queue<TreeNode*> q;
q.push(root);
q.push(nullptr);
while (!q.empty()) {
TreeNode* cur = q.front();
q.pop();
if(cur == nullptr) {
res.insert(res.begin(),v);
v.clear();
if(!q.empty()) {
q.push(nullptr);
}
continue;
}
v.push_back(cur->val);
if(cur->left != nullptr) {
q.push(cur->left);
}
if(cur->right != nullptr) {
q.push(cur->right);
}
}
return res;
}
};
Solution 1.1 ()
class Solution {
public:
vector<vector<int>> levelOrderBottom(TreeNode *root) {
vector<vector<int>> res;
if (root == nullptr) {
return res;
}
queue<TreeNode*> q;
q.push(root);
while (!q.empty()) {
int size = q.size();
vector<int> v;
for (int i = ; i < size; ++i) {
TreeNode* cur = q.front();
q.pop();
if(cur->left != nullptr) {
q.push(cur->left);
}
if(cur->right != nullptr) {
q.push(cur->right);
}
v.push_back(cur->val);
}
res.insert(res.begin(),v);
}
return res;
}
};
递归
Solution 2 ()
【Lintcode】070.Binary Tree Level Order Traversal II的更多相关文章
- 【LeetCode】107. Binary Tree Level Order Traversal II (2 solutions)
Binary Tree Level Order Traversal II Given a binary tree, return the bottom-up level order traversal ...
- 【Lintcode】069.Binary Tree Level Order Traversal
题目: Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to ri ...
- 【LeetCode】107 - Binary Tree Level Order Traversal II
Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left ...
- 【LeetCode】107. Binary Tree Level Order Traversal II 解题报告 (Python&C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:DFS 方法二:迭代 日期 [LeetCode ...
- 【easy】107. Binary Tree Level Order Traversal II 按层输出二叉树
按层输出二叉树,广度优先. 3 / \ 9 20 / \ 15 7 [ [15,7], [9,20], [3] ] /** * Definition for a binary tree node. * ...
- 【一天一道LeetCode】#107. Binary Tree Level Order Traversal II
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 来源: htt ...
- 【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)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 DFS BFS 日期 题目描述 Given a bi ...
随机推荐
- Linux学习日志--共享内存
一:什么是共享内存 共享内存是属于IPC(Inter-Process Communication进程间通信)机制,其它两种是信号量和消息队列,该机制为进程开辟创建了特殊的地址范 ...
- 删除反复行SQL举例
删除反复行SQL实验简单举例 说明:实验按顺序进行.前后存在关联性.阅读时请注意.打开文件夹更便于查看. 构造实验环境: SQL> select count(*) from emp; COU ...
- 小白学习python之路(一):安装python3
引言 作为一个python小白,之前学的是java,不过听说python很流行,功能很强大,可以用很少的代码实现更强的功能,因此我也是被吸引了过来,并且把我的学习经历记录下来. 当然了,要学习 ...
- java代码评审内容
评审内容 u 工具检查 □ Eclipse警告 □ FindBug □ CheckStyle □ Jupiter □ Subclipse 或者Subversive u 代码注释内容(详细参考 ...
- 【Python基础】之函数、类和方法
一.函数 1. def定义函数 Python Shell: def add(a,b): return a+b >>>add(1,2) 3 def add(a=1,b=2): retu ...
- maven项目The superclass "javax.servlet.http.HttpServlet" was not found on the Java Build Path
用Eclipse创建了一个maven web程序,使用tomcat8.5作为服务器,可以正常启动,但是却报如下错误 The superclass "javax.servlet.http.Ht ...
- VMware 报错“Intel VT-x处于禁止状态”
VMware Workstation 10虚拟机安装64位windows server 2008 R2系统时报错“Intel VT-x处于禁止状态”,如下图. 工具/原料 VMware Wor ...
- Tensorflow教程
中文社区 tensorflow笔记:流程,概念和简单代码注释 TensorFlow入门教程集合 tensorboard教程:2017 TensorFlow 开发者峰会 TensorBoard轻松实践 ...
- live555二次开发经验总结:RTSPClient客户端与RTSPServer服务器
live555介绍 安防领域的流媒体开发者估计没有谁不知道live555的,可能并不是因为其架构有多牛,代码有多好看,而是因为这玩意存在的年限实在是太长了,从changelog来看,live555从2 ...
- nexus-2.11.4-01-bundle.tar.gz 下载地址
wget http://sonatype-download.global.ssl.fastly.net/nexus/oss/nexus-2.11.4-01-bundle.tar.gz 注意原本的是ht ...