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】的更多相关文章

  1. 【遍历二叉树】04二叉树的层次遍历【Binary Tree Level Order Traversal】

    ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 给定一个二叉树,返回他的层次遍历的 ...

  2. 【Binary Tree Level Order Traversal】cpp

    题目: Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to ri ...

  3. 【Binary Tree Level Order Traversal II 】cpp

    题目: Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from ...

  4. 【遍历二叉树】05二叉树的层次遍历II【Binary Tree Level Order Traversal II】

    就把vector改成用栈类存放层次遍历的一层的序列 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ ...

  5. 【Binary Tree Post order Traversal】cpp

    题目: Given a binary tree, return the postorder traversal of its nodes' values. For example:Given bina ...

  6. 【一天一道LeetCode】#107. Binary Tree Level Order Traversal II

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 来源: htt ...

  7. 【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 ...

  8. 【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, ...

  9. 【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 ...

随机推荐

  1. how to get iframe dom in js

    how to get iframe dom in js https://stackoverflow.com/questions/3999101/get-iframes-document-from-ja ...

  2. [BinaryTree] AVL树、红黑树、B/B+树和Trie树的比较

    转自:AVL树.红黑树.B/B+树和Trie树的比较 AVL树 最早的平衡二叉树之一.AVL是一种高度平衡的二叉树,所以通常的结果是,维护这种高度平衡所付出的代价比从中获得的效率收益还大,故而实际的应 ...

  3. js遍历数组和遍历对象

    可以用for in来遍历对象,具体内容如下: <script type="text/javascript">             var objs = {      ...

  4. 【CSS】规范大纲

    文件规范: 文件分类 : 通用类 :业务类. 文件引入:行内样式(不推荐):外联引入:内联引入.(避免使用Import引入) 文件本身:文件名. 编码:UTF-8. 注释规范: 块状注释:统一缩进,在 ...

  5. Qt4程序在windows平台下打包发布

    一.打包成绿色版 将源码编译成release版,运行*.exe文件,提示缺少*.dll,在Qt安装目录中找到相应的dll文件(一般在bin目录下),将dll文件复制到exe文件目录下即可. 二.打包成 ...

  6. BZOJ3481 DZY Loves Math III(数论+Pollard_Rho)

    考虑对于每一个x有多少个合法解.得到ax+by=c形式的方程.如果gcd(x,y)|c,则a在0~y-1范围内的解的个数为gcd(x,y).也就是说现在所要求的是Σ[gcd(x,P)|Q]*gcd(x ...

  7. python selenium2 窗口切换实例

    遍历hao123中某一区域的所有链接,点击每个链接时,会打开新的窗口,获取新窗口的title后关闭窗口,切换到初始窗口继续打开下一个链接 代码如下: #coding=utf-8 from seleni ...

  8. SpringBoot之mongoTemplate的使用

    springboot的版本1.5.17.RELEASE. 1.mongo的IP和端口 在resources下的application.properties中加入如下内容 spring.data.mon ...

  9. 初探Java 9 的的模块化

    Java 9中最重要的功能,毫无疑问就是模块化(Module),它将自己长期依赖JRE的结构,转变成以Module为基础的组件,当然这在使用Java 9 开发也和以前有着很大的不同. Java8或更加 ...

  10. 20135306 2.4 ELF文件格式分析

    2.4   ELF文件格式分析 20135306 黄韧 ELF全称Executable and Linkable Format,可执行连接格式,ELF格式的文件用于存储Linux程序.ELF文件(目标 ...