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 ...
随机推荐
- Struts2报错:No result defined for action xxx and result input
case如下: 1. 后台程序要升级, 修改了一些功能,但是没有修改或者添加action的参数. 2. 数据库需要升级,执行了一些sql,修改过action的值. 3. 当修改某个已经存在的记录,然后 ...
- 【转】javascript操作Select标记中options集合
先来看看options集合的这几个方法:options.add(option)方法向集合里添加一项option对象:options.remove(index)方法移除options集合中的指定项:op ...
- 【Luogu】P2057善意的投票(最小割转最大流)
题目链接 也算水题一道吧,不过Round1感性理解一下就xjb建了个图,40 Round2仔细分析了一会,理性建了个图,90 然后分析了半天……改大数组就A了…… 从S到所有值为1的点连一条inf的边 ...
- debian 切换最新源
deb http://ftp.cn.debian.org/debian sid main#deb http://ftp.debian.org/debian/ wheezy main
- centos的iptables设置
首先先说一下iptables是什么东西,可以简单把它理解成一个软件防火墙,一个访问控制列表,规定好哪个端口可以进来东西,哪个端口可以送出东西. 那如果不配置iptables或者iptables配置出错 ...
- BZOJ3930 [CQOI2015]选数 【容斥】
题目 我们知道,从区间[L,H](L和H为整数)中选取N个整数,总共有(H-L+1)^N种方案.小z很好奇这样选出的数的最大公约数的规律,他决定对每种方案选出的N个整数都求一次最大公约数,以便进一步研 ...
- 刷题总结——二逼平衡树(bzoj3224线段树套splay)
题目: Description 您需要写一种数据结构(可参考题目标题),来维护一个有序数列,其中需要提供以下操作:1.查询k在区间内的排名2.查询区间内排名为k的值3.修改某一位值上的数值4.查询k在 ...
- Java 学习(2):java 基础概念
Java作为一种面向对象语言.支持以下基本概念: 多态 继承 封装 抽象 类 对象 实例 方法 重载 基础语法: 一个Java程序可以认为是一系列对象的集合,而这些对象通过调用彼此的方法来协同工作.以 ...
- FZOJ Problem 2110 Star
...
- UVa11361 Investigating Div-Sum Property
数位DP f[位数][自身模k余数][各位数字之和模k余数][当前位是否有上限]=方案数 k<10000,空间不够,如何优化? 不必优化,2^31以内,数字最多只有10位,各位数字之和最多为99 ...