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 ...
随机推荐
- Java的checked exception与unchecked exception
在Java中exception分为checked exception和unchecked异常,两者有什么区别呢? 从表象来看, checked异常就是需要在代码中try ... catch ...的异 ...
- .net core2.0入门使用EF
使用Nuget导入所需要的EF 核心包以及对应数据库的驱动包,我用的是sqlserver(.net 支持的所有数据库) Install-Package Microsoft.EntityFramewor ...
- Python中用dict统计列表中元素出现的次数
01 Python增加元素,不像其他语言使用现实的操作接口,只需要dict[1]=3,如果字典中不存在1,则直接新增元素键值对(1,3),如果存在则替换键1为3. if key in dict:判断出 ...
- Java pdf转String 并修正格式
在尝试pdf转成String的时候,首先用python的pdfminer和pdfminer3k去尝试转换,然后资料看不太懂,就尝试用了java, 以下是java的pdfbox写的pdf转String函 ...
- MugLife app是一款可以将静态照片变成3D动画的手机应用
MugLife app是一款可以将静态照片变成3D动画的手机应用,如下效果图所示: 大家可以看到,这个静态图具有了类3D的动画特效,是不是很好玩? 这种算法是如何实现的呢? 这里给出一篇论文“Brin ...
- BZOJ2276 [Poi2011]Temperature 【单调队列】
题目链接 BZOJ2276 题解 一开始看错题,以为求的是可以不连续的,想出一个奇怪的线段树,发现空间根本开不下?? 题目要我们求连续的最长可能不下降区间 对于区间\([l,r]\)如果合法,当且仅当 ...
- 【poj3623】 Best Cow Line, Gold
http://poj.org/problem?id=3623 (题目链接) 题意 给出一个字符串,每次可以取首或尾接到一个新的字符串后面,求构出的字典序最小的新字符串. Solution 首先可以发现 ...
- elasticsearch5使用snapshot接口备份索引
数据备份是一个必须要考虑的问题,官网提供了 snapshot 接口来备份和恢复数据. 先来看看官方说明: 如果ES是集群,那么需要使用共享存储,支持的存储有: a.shared file system ...
- MVC 中@Html.DropDownListFor() 设置选中项 这么不好使 ? [问题点数:40分,结帖人lkf181]
http://bbs.csdn.net/topics/390867060 由于不知道错误原因在哪 我尽量把代码都贴出来吧:重点是:在 Controller 类里 我给 SelectListItem集合 ...
- 文档比较比对工具Beyond Compare
Beyond Compare 可以比较文件夹或文件