【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 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.
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
\
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> > levelOrderBottom(TreeNode *root) {
// Note: The Solution object is instantiated only once and is reused by each test case.
vector<vector<int> > res;
if(root == NULL)
return res;
queue<TreeNode *> qu;
qu.push(root);
qu.push(NULL);
vector<int> onelevel;
while(true)
{
TreeNode *cur = qu.front();
qu.pop();
if(cur == NULL)
{
res.push_back(onelevel);
onelevel.clear();
if(qu.empty())
break;
qu.push(NULL);
}
else
{
onelevel.push_back(cur->val);
if(cur->left)
qu.push(cur->left);
if(cur->right)
qu.push(cur->right);
} }
reverse(res.begin(),res.end());
return res;
}
};
【LeetCode】Binary Tree Level Order Traversal II的更多相关文章
- 【leetcode】Binary Tree Level Order Traversal I & II
Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, ...
- 【Leetcode】【Easy】Binary Tree Level Order Traversal II
Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left ...
- 【题解】【BT】【Leetcode】Binary Tree Level Order Traversal
Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, ...
- 【Leetcode】Binary Tree Level Order Traversal
Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, ...
- 【LeetCode】Binary Tree Level Order Traversal 【BFS】
Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, ...
- 【LeetCode】Binary Tree Level Order Traversal(二叉树的层次遍历)
这道题是LeetCode里的第102道题. 题目要求: 给定一个二叉树,返回其按层次遍历的节点值. (即逐层地,从左到右访问所有节点). 例如: 给定二叉树: [3,9,20,null,null,15 ...
- 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 ...
随机推荐
- Cocoapod错误 - Xcode6.4
当我从Xcode 6.3切换到Xcode6.4的时候,因为我是mac上安装了两个不同的版本,现在把Xcode 6.3卸掉了. 现在再次运行pod install命令的时候,提示如下错误: Upda ...
- Java精确计算
Java精确计算 如果我们编译运行下面这个程序会看到什么? public class Test{ public static void main(String args[]){ System.out. ...
- WordPress 前端投稿/编辑插件 DJD Site Post(支持游客和已注册用户)
转自:http://www.wpdaxue.com/front-end-publishing.html 说到前端用户投稿,倡萌之前推荐过3个不错的插件: WordPress匿名投稿插件:DX-Cont ...
- bzoj 1964: hull 三维凸包 计算几何
1964: hull 三维凸包 Time Limit: 1 Sec Memory Limit: 64 MBSubmit: 54 Solved: 39[Submit][Status][Discuss ...
- [BZOJ 3531] [Sdoi2014] 旅行 【离线+LCT】
题目链接:BZOJ - 3531 题目分析 题目询问一条路径上的信息时,每次询问有某种特定的文化的点. 每个点的文化就相当于一种颜色,每次询问一条路径上某种颜色的点的信息. 可以使用离线算法, 类似于 ...
- hdu 4751
一道很简单的题,不过在比赛的时候没有写出来: 刚刚看到这个题,我以为是一个图论题,后来发现其实就是一个暴力的题: 用bfs,因为一个人与他不认识的人肯定不会在一个集合,如果判断出现冲突则分配失败,否则 ...
- 一周一话题之四(JavaScript、Dom、jQuery全面复习总结<js篇>)
-->目录导航 一. JavaScript 1. js介绍 2. js语法 3. js进阶 4. js高级 5. 事例代码下载 一. JavaScript 做BS系统,JavaScript的使用 ...
- 李洪强iOS开发之-环信02.2_环信官网下载环信 SDK
李洪强iOS开发之-环信02.2_环信官网下载环信 SDK 移动客服即时通讯云 iOS SDK 当前版本:V3.1.4 2016-07-08 [ 版本历史 ] | 开发指南 | 知识库 | Demo源 ...
- 【Xamarin挖墙脚系列:在VMware11中安装Mac10.11 EI Captain后的vmware tools】
原文:[Xamarin挖墙脚系列:在VMware11中安装Mac10.11 EI Captain后的vmware tools] 如何安装 darwin.iso,百度去吧. 关键是对应版本的darwin ...
- selectpicker下拉多选框ajax异步或者提前赋值=》默认值
Bootstrap select多选下拉框赋值 success: function (data) { var oldnumber = new Array(); $.each(data, functio ...