binary-tree-level-order-traversal I、II——输出二叉树的数字序列
I
Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, level by level).
For example:
Given binary tree{3,9,20,#,#,15,7},
3
/ \
9 20
/ \
15 7
return its level order traversal as:
[
[3],
[9,20],
[15,7]
]
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> > levelOrder(TreeNode *root) {
vector<vector<int>> res;
if(root==NULL) return res;
queue<TreeNode*> q;
q.push(root);
while(!q.empty()){
int n=q.size();
vector<int> v;
for(int i=;i<n;i++){
TreeNode *cur=q.front();
q.pop();
v.push_back(cur->val);
if(cur->left!=NULL)
q.push(cur->left);
if(cur->right!=NULL)
q.push(cur->right);
}
res.push_back(v);
}
return res;
}
};
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.
先将结果v存入stack中,最后在从stack倒入res形成倒序,未找到其他好的方法
/**
* 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>> res;
if(root==NULL) return res;
stack<vector<int>> s;
queue<TreeNode*> q;
q.push(root);
while(!q.empty()){
int n=q.size();
vector<int> v;
for(int i=;i<n;i++){
TreeNode *cur=q.front();
q.pop();
v.push_back(cur->val);
if(cur->left!=NULL)
q.push(cur->left);
if(cur->right!=NULL)
q.push(cur->right); }
s.push(v);
}
while(!s.empty()){
res.push_back(s.top());
s.pop();
}
return res;
}
};
binary-tree-level-order-traversal I、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:Binary Tree Level Order Traversal I II
		LeetCode:Binary Tree Level Order Traversal Given a binary tree, return the level order traversal of ... 
- Binary Tree Level Order Traversal,层序遍历二叉树,每层作为list,最后返回List<list>
		问题描述: Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to ... 
- 102/107. Binary Tree Level Order Traversal/II
		原文题目: 102. Binary Tree Level Order Traversal 107. Binary Tree Level Order Traversal II 读题: 102. 层序遍历 ... 
- 35. Binary Tree Level Order Traversal  &&  Binary Tree Level Order Traversal II
		Binary Tree Level Order Traversal OJ: https://oj.leetcode.com/problems/binary-tree-level-order-trave ... 
- Binary Tree Level Order Traversal,Binary Tree Level Order Traversal II
		Binary Tree Level Order Traversal Total Accepted: 79463 Total Submissions: 259292 Difficulty: Easy G ... 
- 【一天一道LeetCode】#107. Binary Tree Level Order Traversal II
		一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 来源: htt ... 
- LeetCode之“树”:Binary Tree Level Order Traversal && Binary Tree Level Order Traversal II
		Binary Tree Level Order Traversal 题目链接 题目要求: Given a binary tree, return the level order traversal o ... 
- 【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 ... 
- [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 ... 
随机推荐
- sqlserver数据库的权限设置
			1.先用Windows账户登陆,然后在安全性中添加用户--SQL server 身份验证,用户名,密码2.用户映射--勾选对应的数据库--数据库角色成员身份--db_owner public 
- Winform 连接Oracle10g时出错的解决方法
			环境:Win7(64位). VS2012 update3.Oracle10 (本机已安装ODTwithODAC1120320_32bit) 最近在开发一程序时莫名其妙报一个错误(未能加载文件或程序集“ ... 
- Welcome-to-Swift-13继承(Inheritance)
			一个类可以继承(inherit)另一个类的方法(methods),属性(property)和其它特性.当一个类继承其它类时,继承类叫子类(subclass),被继承类叫超类(或父类,superclas ... 
- 用meta标签让网页用360打开时默认为极速模式
			最近做项目,用360浏览器访问自己的本地网页,发现都是默认在兼容模式下打开,做的淡入淡出轮播效果在兼容模式下看时,感觉切换很生硬.百度,发现360官网帮助里有说明用meta标签控制浏览器内核,网址为h ... 
- 刷题总结——松鼠的新家(bzoj3631)
			题目: Description 松鼠的新家是一棵树,前几天刚刚装修了新家,新家有n个房间,并且有n-1根树枝连接,每个房间都可以相互到达,且俩个房间之间的路线都是唯一的.天哪,他居然真的住在“树”上. ... 
- scrapy之Selectors
			练习url:https://doc.scrapy.org/en/latest/_static/selectors-sample1.html 一 获取文本值 xpath In []: response. ... 
- APUE 学习笔记(五)  进程环境
			1.main函数 C程序总是从main函数开始执行,当内核执行C程序时,在调用main函数之前先调用exec函数从内核获取命令行参数和环境变量值 2.进程终止 正常终止: (1)在main函数内执 ... 
- net4:GridView中的重要操作(添加checkbox,以及鼠标动作,行颜色等)
			原文发布时间为:2008-07-29 -- 来源于本人的百度文章 [由搬家工具导入] using System;using System.Data;using System.Configuration ... 
- UML系列,使用UML实现GOF Design patterns,常用模式类图解读
			1.单例:Singleton, DirectedAssociation 
- 标准C程序设计七---56
			Linux应用 编程深入 语言编程 标准C程序设计七---经典C11程序设计 以下内容为阅读: <标准C程序设计>(第7版) 作者 ... 
