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. iPhoneX设计尺寸和适配

    被iPhone X刷了一天屏,到下午实在受不了各种假帖.标题写着“iPhone X 适配.指南.设计稿” 内容却是发布会回顾和手机介绍.索性自己去官网找素材写一篇只针对iPhone X适配的贴子,与设 ...

  2. MachineLearning Exercise 4 :Neural Networks Learning

    nnCostFunction 消耗公式: a1 = [ones(m,) X]; z2 = a1*Theta1'; pre = sigmoid(a1*Theta1'); a2 = [ones(m,) p ...

  3. java.io.FileNotFoundException: generatorConfig.xml (系统找不到指定的文件。)

    在使用MyBatis的逆向工程生成代码时,一直报错java.io.FileNotFoundException: generatorConfig.xml (系统找不到指定的文件.),如图 文件结构如下: ...

  4. Python学习---日期时间

    在Python里面日期时间的功能主要由几个模块提供:time,calendar,datetime,date等 time主要用到的功能函数: #!/usr/bin/python3 # coding:ut ...

  5. 20135239 Linux内核分析 期中总结

    期中总结 链接 网易云课堂 MOOC 摘录与实验内容 第一周: http://www.cnblogs.com/20135239-yxlm/p/5216842.html 第二周: http://www. ...

  6. 解题:SDOI 2014 数表

    题面 为了好写式子,先不管$a$的限制 设$facs$为因子和,那么有 $ans=\sum\limits_{i=1}^n\sum\limits_{j=1}^mfacs(gcd(i,j))$ 再设$f( ...

  7. bzoj 3779: 重组病毒

    一道好题~~ 一个点到根传染需要的时间是这段路径上不同颜色的数目,一个点子树到根平均传染时间就是加权平均数了(好像是废话). 所以只要用线段树维护dfs序就这个可以了,换根的话一个点的子树要么在dfs ...

  8. fzyzojP2984 -- 序列变换问题

    一个区间缩小变换的问题,并且n<=300 启示我们区间dp 我们考虑最后一定是在原串上扣一些,剩一些 所以不妨前求出[l,r]把[l,r]完全处理成什么样子的方案数 然后再来一遍序列dp,统计答 ...

  9. Jenkins(三)---Jenkins初始配置和插件配置

    从Jenkins(二)中可以知道 jenkins 的工作目录为/opt/jenkins [很重要!!!][很重要!!!][很重要!!!]在配置此目录以前,将这两台的主机进行配置为ssh root用户无 ...

  10. CDOJ--1056

    原题链接:http://acm.uestc.edu.cn/problem.php?pid=1056 题目:大小写切换 分析:以前这种问题我都是用dp写的,最近学到了一种更简洁的方法,特此记录下来! # ...