【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 ...
随机推荐
- javascript 事件多次绑定和删除
同一个事件绑定多个事件处理程序(适合自己写)IE: 添加: 对象.attachEvent("on事件名","处理程序/函数名"); 执行顺序从后向前 删除: 对 ...
- Python中的redis学习笔记
redis是一个key-value结构的数据库,value的格式可以使string,set,list,map(即python里面的dict),sorted set(有序集合) 1.初始化 1)直接连接 ...
- 跨线程操作UI控件
写程序的时候经常会遇到跨线程访问控件的问题,看到不少人去设置Control.CheckForIllegalCrossThreadCalls = false;这句话是告诉编译器不要对跨线程访问作检查,可 ...
- 查找类class所在的jar包
CTRL+SHIFT+R 意思是输入关键字搜索当前工程的 对象. CTRL+SHIFT+T 输入关键字搜索当前工程的类.
- web design tools
https://www.google.com/webdesigner/ http://html.adobe.com/edge/inspect/ http://www.creativebloq.com/ ...
- NET笔记——Delegate
对于初学者,委托是很容易让人晕的,一是晕它如何起作用,二是晕它有什么用. 最近回过头来又看了下委托,又有些不同的感觉,写之自用. 声明方面,委托可以被声明在类内,也可以与类同级,并且声明时没有方法体: ...
- iOS面试题16719-b
1. 反转二叉树,不用递归 /*** Definition for a binary tree node.* public class TreeNode {* int val;* Tr ...
- 个人笔记--Servlet之过滤器实现权限拦截
一.编写一个Java类实现javax.servlet.Filter接口 package cn.edu.sxu.filter; import java.io.IOException; import ja ...
- c/c++ 传统数组的缺点
专题: 动态内存分配 (所有高级语言,没有C里深刻) 传统数组的缺点: 1.数组长度必须事先指定,而且只能是常整数,不能是变量 例子 ]; //必须事先指定,而且只能是常整数 ; int a ...
- What does it mean for an algorithm to be fair
What does it mean for an algorithm to be fair In 2014 the White House commissioned a 90-day study th ...