【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 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 7
return 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.
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
\
5
The above binary tree is serialized as "{1,2,3,#,#,4,#,#,5}"
.
/**
* 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> > result; void levelTra(TreeNode *root, int level)
{
if(root == NULL)
return;
if(level == result.size())
{
vector<int> v;
result.push_back(v);
}
result[level].push_back(root->val);
levelTra(root->left, level+);
levelTra(root->right, level+);
} vector<vector<int> > levelOrderBottom(TreeNode *root)
{
levelTra(root, );
return vector<vector<int> >(result.rbegin(), result.rend());
}
};
解法二:普通层次遍历后逆序。
/**
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/ struct Node
{
TreeNode* tNode;
int level;
Node(TreeNode* newtNode, int newlevel): tNode(newtNode), level(newlevel) {}
}; class Solution {
public:
vector<vector<int> > levelOrderBottom(TreeNode *root) {
vector<vector<int> > ret;
if(!root)
return ret;
// push root
Node* rootNode = new Node(root, );
queue<Node*> Nqueue;
Nqueue.push(rootNode); vector<int> cur;
int curlevel = ;
while(!Nqueue.empty())
{
Node* frontNode = Nqueue.front();
Nqueue.pop(); if(frontNode->level > curlevel)
{
ret.push_back(cur);
cur.clear();
curlevel = frontNode->level;
} cur.push_back(frontNode->tNode->val); if(frontNode->tNode->left)
{
Node* leftNode = new Node(frontNode->tNode->left, frontNode->level+);
Nqueue.push(leftNode);
}
if(frontNode->tNode->right)
{
Node* rightNode = new Node(frontNode->tNode->right, frontNode->level+);
Nqueue.push(rightNode);
}
}
ret.push_back(cur); reverse(ret.begin(), ret.end());
return ret;
}
};
【LeetCode】107. Binary Tree Level Order Traversal II (2 solutions)的更多相关文章
- 【LeetCode】107. Binary Tree Level Order Traversal II 解题报告 (Python&C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:DFS 方法二:迭代 日期 [LeetCode ...
- 【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
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 来源: htt ...
- 【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】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 二叉树的层序遍历 (Python&C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 BFS DFS 日期 题目地址:https://lee ...
- 【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 ...
- LeetCode OJ 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 ...
随机推荐
- js读取json方法
json也是对象,可以直接使用对象调用 var json = {contry:{area:{man:"12万",women:"10万"}}}; json.con ...
- Java开发牛人十大必备网站
以下是我收集的Java开发牛人必备的网站.这些网站可以提供信息,以及一些很棒的讲座, 还能解答一般问题.面试问题等.质量是衡量一个网站的关键因素,我个人认为这些网站质量都很好.接下来,我会跟大家分享我 ...
- HTTP协议状态码详解(HTTP Status Code)(转)
原文链接:HTTP协议状态码详解(HTTP Status Code) 使用ASP.NET/PHP/JSP 或者javascript都会用到http的不同状态,一些常见的状态码为: 200 – 服务器成 ...
- JDBC结合JSP使用(1)
1. 添加数据 在jsp页面中添加数据,和在serv中添加数据相似.获得页面中提交的数据以后,把数据保存到数据库表中,JSP的代码如下: add.jsp <%@ page language=&q ...
- C语言:通过指针函数输出二维数组中每个学生的成绩
// // main.c // Pointer_function // // Created by ma c on 15/8/2. // Copyright (c) 2015年 bjsxt. ...
- 第二章 Jackson属性名转换+属性忽略
@Data @JsonIgnoreProperties(ignoreUnknown = true) public class MyRecord { private boolean succeed; p ...
- Android -- 拷贝assets下的资源文件到SD卡中(可以超过1M)
很多手机游戏,在安装APK之后都得需要下载相应的资源包,然后才能进入游戏. 有这样一个需求:就是游戏中需要的资源包打在APK内,随apk一起进行安装到手机中. 这样就不需要,在安装APK之后,去下载资 ...
- Linux之nohup命令:实现退出终端后程序继续后台运行
转自:http://tech.ccidnet.com/art/302/20070618/1115599_1.html 简单而有用的nohup命令在UNIX/LINUX中,普通进程用&符号放到后 ...
- listView/GridView getChild获取不到的解决方法
在onCreate或onResume中调用了getChildAt()方法,这时候adapter中的Item还没有放入到AdapterView中去.... 解决方法,当activity获得焦点事件的时候 ...
- windows server 2012 IE增强的安全配置如何关闭
http://jingyan.baidu.com/article/6181c3e076ac0b152ff15354.html 打开左下角的 服务端 关闭这个就可以了