[leetcode] 7. 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).
For example:
Given binary tree{3,9,20,#,#,15,7},3
/ \
9 20
/ \
15 7return its bottom-up level order traversal as:
[
[15,7],
[9,20],
[3]
]confused what
"{1,#,2,3}"means? > read more on how binary tree is serialized on OJ.OJ's Binary Tree Serialization:
The serialization of a binary tree follows a level order traversal, where '#' signifies a path terminator where no node exists below.
Here's an example:
1
/ \
2 3
/
4
\
5The above binary tree is serialized as
"{1,2,3,#,#,4,#,#,5}".
就是按照层数来输出树。思路分为三步,先拿到树的最大层数len,然后再写一个拿指定层的各元素的函数,最后按题目要求写一个返回vector<vector<int>>的函数,里面用一个循环来多次调用之前那个函数,好了,题解如下:
/**
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
vector<vector<int> > levelOrderBottom(TreeNode *root)
{
vector<vector<int>> temp;
int len = MaxDepth(root); for (int i = len - 1; i >= 0; i--)
{
vector<int> level;
getElement(level, 0, i, root); temp.push_back(level);
level.clear();
} return temp; } int MaxDepth(TreeNode *temp)
{
if (temp == NULL)
return 0;
else
{
int aspros = MaxDepth(temp->left);
int defteros = MaxDepth(temp->right);
return 1 + (aspros>defteros ? aspros : defteros);
}
} void getElement(vector<int> &level, int count, int len, TreeNode *root)
{
if (root != NULL)
{
if (count == len)
{
level.push_back(root->val);
}
getElement(level, count + 1, len, root->left);
getElement(level, count + 1, len, root->right);
}
}
};
因为题目要求是要从底部向上输出,所以在主函数的for循环里用了倒序。
这次的blog用Live Writer发布,测试一下。
[leetcode] 7. Binary Tree Level Order Traversal II的更多相关文章
- 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】Binary Tree Level Order Traversal II
Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left ...
随机推荐
- 进程基本-进程创建,僵尸进程,exec系列函数
Linux系统中,进程的执行模式划分为用户模式和内核模式,当进程运行于用户空间时属于用户模式,如果在用户程序运行过程中出现系统调用或者发生中断事件,就要运行操作系统(即核心)程序,进程的运行模式就变为 ...
- RecordingOptions录制设置选项
1.录制思考时间 2.录制方式 3.自定义证书 4.非资源选项
- 1021 docker搭建mysql、网络模式、grid
1.搭建并连接mysql服务 1.1.mysql官方命令 https://hub.docker.com/_/mysql/ #下载mysql镜像: docker pull mysql #启动mysql: ...
- mysql错误:Can’t create TCP/IP socket (10106) 解决方法
错误描述 “mysql错误:Can’t create TCP/IP socket (10106)”,目测是socket端口被占用的原因,然后在打开tomcat,报的错误中也包含了“socket”,再一 ...
- 源文件封装为IP的步骤
因为模块的交接,最好将写好的源文件和生成的IP封装一个IP,然后再转交给其他的同事使用,这是一种好的习惯.但是对于,封装的过程还是需要注意一下.实际的看看步骤吧.1)将源文件和使用到的IP生成工程. ...
- django-聚合操作
聚合操作就是对数据库的数值类型操作的方法 avg,sum,max,min,count select avg(age) from students # 求年龄平均值 django中的聚合操作 1.a ...
- three3D地图
终于闲下来了,前段时间做了个项目,高精度精准定位系统,用到了three3D地图 听起来是不是很炫酷?其实并不难.先把部分代码附上(文件比较多,只粘贴部分的) $(function () { i ...
- tensorflow 基本函数(1.tf.split, 2.tf.concat,3.tf.squeeze, 4.tf.less_equal, 5.tf.where, 6.tf.gather, 7.tf.cast, 8.tf.expand_dims, 9.tf.argmax, 10.tf.reshape, 11.tf.stack, 12tf.less, 13.tf.boolean_mask
1. tf.split(3, group, input) # 拆分函数 3 表示的是在第三个维度上, group表示拆分的次数, input 表示输入的值 import tensorflow ...
- 迷你MVVM框架 avalonjs 1.2.4发布
这段时间一直忙于建立avalon的单元测试,与重构官网.对avalon的更新都是来自公司内部的需求,性能优化与一些BUG修复. 添加大量调试日志. 重构shimController,以提高性能. cr ...
- C++ 20170807
mesos/3rdparty/stout/include/stout/err.hpp=======================================================str ...