2016.6.24——vector<vector<int>>【Binary Tree Level Order Traversal】
Binary Tree Level Order Traversal
本题收获:
1.vector<vector<int>>的用法
vector<vector<int> >注意<int>后面的空格,vector<vector<int>>表示的是二位向量。
输出格式(后面代码),不知道大小时,在vector中用push_back(vector<int>())
2.树用迭代
题目:
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,null,null,15,7]
,
3
/ \
9 20
/ \
15 7
return its level order traversal as:
[
[3],
[9,20],
[15,7]
]
思路:
:1.用vector<vector<int>>输出二位数组 2.迭代。
代码:
vector<vector<int>> ret; void buildVector(TreeNode *root, int depth)
{
if(root == NULL) return;
if(ret.size() == depth)
ret.push_back(vector<int>()); //depth的设置很巧妙 ret[depth].push_back(root->val);
buildVector(root->left, depth + );
buildVector(root->right, depth + );
} vector<vector<int> > levelOrder(TreeNode *root) {
buildVector(root, );
return ret;
}
vector<vector<int>>输出格式:
int n = res.size();
for (int i = ; i < n; i++)
{
int m = res[i].size(); //注意vector<vector<int> >的输出,以及size是怎么设定
for (int j = ; j < m; j++)
{
cout << res[i][j] << " ";
}
cout << endl; //输出格式 cout << endl的位置
}
全部测试代码:
// Binary Tree Level Order Traversal.cpp : 定义控制台应用程序的入口点。
// #include "stdafx.h"
#include "iostream"
#include "malloc.h"
#include "vector"
using namespace std; struct TreeNode
{
int val;
TreeNode *left, *right;
TreeNode(int x) : val(x), left(NULL), right(NULL){};
}; class MyClass
{
public:
vector<vector<int> > res;
vector<vector<int>> levelOrder(TreeNode* root)
{ buildvector(root, );
return res;
} void buildvector(TreeNode* root, int depth)
{
if (root == NULL) return;
if (res.size() == depth)
{
res.push_back(vector<int>());
} res[depth].push_back(root->val);
buildvector(root->left, depth + );
buildvector(root->right, depth + );
}
}; void creatTree(TreeNode* &T)
{
int data;
cin >> data;
if (data == -)
{
T = NULL;
}
else
{
T = (TreeNode*)malloc(sizeof(TreeNode));
T->val = data;
creatTree(T->left);
creatTree(T->right);
}
} int _tmain(int argc, _TCHAR* argv[])
{
TreeNode* root = NULL;
creatTree(root);
vector<vector<int> > res;
MyClass solution;
res = solution.levelOrder(root);
int n = res.size();
for (int i = ; i < n; i++)
{
int m = res[i].size(); //注意vector<vector<int> >的输出,以及size是怎么设定
for (int j = ; j < m; j++)
{
cout << res[i][j] << " ";
}
cout << endl; //输出格式
}
system("pause");
return ;
}
3
/ \
9 20
/ \
15 7
上面的树,在本题作为输入为(3 9 -1 -1 20 15 -1 -1 7 -1 -1)
-1代表后面没有子节点。
2016.6.24——vector<vector<int>>【Binary Tree Level Order Traversal】的更多相关文章
- 【遍历二叉树】04二叉树的层次遍历【Binary Tree Level Order Traversal】
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 给定一个二叉树,返回他的层次遍历的 ...
- 【Binary Tree Level Order Traversal】cpp
题目: Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to ri ...
- 【Binary Tree Level Order Traversal II 】cpp
题目: Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from ...
- 【遍历二叉树】05二叉树的层次遍历II【Binary Tree Level Order Traversal II】
就把vector改成用栈类存放层次遍历的一层的序列 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ ...
- 【Binary Tree Post order Traversal】cpp
题目: Given a binary tree, return the postorder traversal of its nodes' values. For example:Given bina ...
- 【一天一道LeetCode】#107. Binary Tree Level Order Traversal II
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 来源: htt ...
- 【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 ...
- 【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】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 ...
随机推荐
- YARN结构分析与工作流程
YARN Architecture Link: http://hadoop.apache.org/docs/r2.7.2/hadoop-yarn/hadoop-yarn-site/YARN.html ...
- rabbitmq 集群安装
1.安装模式分为三种:单主机模式.普通集群模式.镜像集群模式. 单主机模式:rabbitmq运行在一台主机上,生产环境不建议使用该模式,性能有限.如果该台主机down机,整个服务将不可用. 普通集群模 ...
- python selenium wait方法
遇到一个网站运行很慢,所以要等待某个元素显示出来之后再进行操作,自己手上的书上没有例子可以直接用 发现一篇文章:http://www.cnblogs.com/yoyoketang/p/6517477. ...
- Just Another Problem UVA - 11490(枚举)
题意: 你有s个士兵,并打算把他们排成一个r行c列,但有两个"洞"的矩形方队,以迷惑敌人(从远处看,敌人可能误以为一共有r*c个士兵).洞是两个大小相同的正方形,为了隐蔽性更强,方 ...
- git gitignore 如何添加,为何添加了无效
需求:一个新项目源码要挂载在GIT服务器上,但是里面的obj文件夹,bin文件夹,.exe文件不提交(每次) 有两种情况出现 1.项目初始化的时候就加入拦截规则文件 gitignore 具体步骤请参 ...
- uoj259 & 独立集问题的一些做法
很早以前就做了一遍这题,当时好像啥都不会,今天重做一下. 这个题题意简单地说就是输入k.p和一个图,求图大小为k的独立集个数mod p. subset1.in n=24,m=19,k=8 subse ...
- MugLife app是一款可以将静态照片变成3D动画的手机应用
MugLife app是一款可以将静态照片变成3D动画的手机应用,如下效果图所示: 大家可以看到,这个静态图具有了类3D的动画特效,是不是很好玩? 这种算法是如何实现的呢? 这里给出一篇论文“Brin ...
- java 面试题 -- 线程 按序 交替
编写一个程序,开启 3 个线程,这三个线程的 ID 分别为A.B.C,每个线程将自己的 ID 在屏幕上打印 10 遍,要求输出的结果必须按顺序显示.如:ABCABCABC…… 依次递归? packag ...
- [CQOI2016] 手机号码 (数位dp)
link $solution:$ $10^{10} \leq L \leq R < 10^{11}$这个数据范围很容易想到数位$dp$. 依照题意模拟即可. #include<iostre ...
- centos6.5搭建LVS+Keepalived
1.配置LVS负载调度器 (1)为eth0配置IP地址,为eth0:0配置VIP地址. vi /etc/sysconfig/network-scripts/ifcfg-eth0 …… DEVICE=e ...