[LeetCode] 107 Binary Tree Level Order Traversal II (easy)
原题
层序遍历,从自底向上按层输出。 左→右→中
解法一 :
DFS,求出自顶向下的,最后返回时反转一下。
class Solution
{
public:
vector<vector<int>> res;
vector<vector<int>> levelOrderBottom(TreeNode *root)
{
int level = 0;
dfs(root, level);
reverse(res.begin(), res.end());
return res;
}
void dfs(TreeNode *root, int level)
{
if (root == NULL)
return;
if (level == res.size())
res.push_back({});
dfs(root->left, level + 1);
dfs(root->right, level + 1);
res[level].push_back(root->val);
}
};
解法二:
BFS
class Solution
{
public:
vector<vector<int>> res;
vector<vector<int>> levelOrderBottom(TreeNode *root)
{
if (root == NULL)
return res;
int level = 0;
queue<TreeNode *> que;
que.push(root);
while (!que.empty())
{
vector<int> cur;
int level = que.size();
for (int i = 0; i < level; i++)
{
TreeNode *temp = que.front();
que.pop();
cur.push_back(temp->val);
if (temp->left != NULL)
que.push(temp->left);
if (temp->right != NULL)
que.push(temp->right);
}
res.insert(res.begin(), cur);
}
return res;
}
};
[LeetCode] 107 Binary Tree Level Order Traversal II (easy)的更多相关文章
- Java for 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(二叉树的层级顺序遍历2)(*)
翻译 给定一个二叉树,返回从下往上遍历经过的每一个节点的值. 从左往右,从叶子到节点. 比如: 给定的二叉树是 {3,9,20,#,#,15,7}, 3 / \ 9 20 / \ 15 7 返回它从下 ...
- [LeetCode] 107. Binary Tree Level Order Traversal II 二叉树层序遍历 II
Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left ...
- (二叉树 BFS) 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 (二叉树阶层顺序遍历之二)
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 ----- java
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
Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left ...
- Java [Leetcode 107]Binary Tree Level Order Traversal II
题目描述: Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, fro ...
- leetcode 107.Binary Tree Level Order Traversal II 二叉树的层次遍历 II
相似题目: 102 103 107 /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode ...
随机推荐
- Qt for Android之Hello World
Qt for Android (Hello World APK 创建)Qt是跨平台的,如桌面.移动.嵌入式平台.Qt for Android可以在Android v2.3.3 (API level 1 ...
- Liferay6.1 配置友好的URL映射
说明:以下内容和官方文档相差不大,如果您英文较好,建议直接去读官方文档,地址是:https://dev.liferay.com/develop/tutorials/-/knowledge_base/6 ...
- 基于jsp技术的校园二手交易网站
[项目介绍]基于jsp的校园二手商品交易网站系统使用jsp技术进行开发,项目主要实现了一整套的校园二手交易逻辑, 主要功能如下(包括但不限于,只列出主要功能): 管理员模块 |-----用户管理 ...
- python Trojan 模块(我忘记几了)—— 通信隧道建立
0X01 SSH的建立 我想,第一步先实现简单的ssh通信再说,类似其他那种高端的C&C将逐步研究 对于ssh,python有个module叫paramiko,对没错看起来像日语单词 The ...
- 每周分享五个 PyCharm 使用技巧(五)
文章首发于 微信公众号:Python编程时光 大家好,这是本系列 PyCharm 的高效使用技巧的第五篇.按照惯例,本次还是分享 5 个. 本系列前四篇如下,若还没看的,你可以点击查阅 21. 随处折 ...
- Hive学习之路(二)—— Linux环境下Hive的安装部署
一.安装Hive 1.1 下载并解压 下载所需版本的Hive,这里我下载版本为cdh5.15.2.下载地址:http://archive.cloudera.com/cdh5/cdh/5/ # 下载后进 ...
- ABP开发框架前后端开发系列---(13)高级查询功能及界面的处理
在一般的检索界面中,基于界面易用和美观方便的考虑,我们往往只提供一些常用的条件查询进行列表数据的查询,但是有时候一些业务表字段很多,一些不常见的条件可能在某些场景下也需要用到.因此我们在通用的查询条件 ...
- 【JVM】02垃圾回收机制
垃圾回收 垃圾回收策略https://blog.csdn.net/u010425776/article/details/51189318 程序计数器.Java虚拟机栈.本地方法栈都是线程私有的,也就是 ...
- CI框架中的奇葩
今天在win下开发,使用ci框架,本来是没有任何问题,然后转向了mac上开发,结果出现了个奇葩的问题,就是在ci框架中,控制器命名以"Admin_"为前缀的,在url中,控制器也必 ...
- MAC iterm2 常用快捷键大全
标签 新建标签:command + t 关闭标签:command + w 切换标签:command + 数字 / command + 左右方向键 切换全屏:command + enter 查找:com ...