【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 ...
随机推荐
- css 使表格随着内容自动适应宽度
所谓难而不会,会儿不难.这个问题让我纠结了很长时间,一句css解决了,仅仅靠一个属性 td { white-space: nowrap; }
- 记录下关于ejabberd及XMPP的官网链接
ejabberd中文翻译 ——http://wiki.jabbercn.org/Ejabberd2:安装和操作指南 XMPP中文翻译: http://wiki.jabbercn.org/XEP-012 ...
- android WebView详细使用方法(转)
1.最全面的Android Webview详解 2.最全面总结 Android WebView与 JS 的交互方式 3.你不知道的 Android WebView 使用漏洞 如果想保证登录状态,就插入 ...
- CountDownTimer
package com.daoge.widget; import java.text.DecimalFormat; import android.os.CountDownTimer; import a ...
- mysqldump导入导出数据库总结
mysqldump导入导出数据库总结 基本的使用方法总结: 1 导出所有库 系统命令行 mysqldump -uusername -ppassword --all-databases > all ...
- 九度OJ 1154:Jungle Roads(丛林路径) (最小生成树)
时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:832 解决:555 题目描述: The Head Elder of the tropical island of Lagrishan has ...
- vscode设置默认shell 快速到行
vscode设置默认shell - CSDN博客 https://blog.csdn.net/butterfly5211314/article/details/78944805 在文件 -> 首 ...
- cocos2d-js添加道有道插屏(通过jsb反射机制)
1.导入jar包 2.修改AndroidManifest.xml文件 添加权限: <activity android:configChanges="keyboard|keyb ...
- NFT是什么,有什么前景?
去年 11 月,Crypokitties 的发布给加密货币的世界带来了风暴,有些加密猫的价格甚至涨到了 30 万美元,以太坊网络拥堵不堪,平均贡献了当时以太坊网络30%的交易额.当 Cryptokit ...
- Zookeeper 伪分布式部署
Zookeeper 可以通过配置不同的配置文件启动 部署环境:CentOS 6.7 Zookeeper 路径: /opt/htools/zookeeper-3.4.6 操作步骤: 1 复制三份zoo. ...