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. 使用docker部署项目

    一.Dockerfile编写 FROM hub.c.163.com/library/java:8-alpine ADD target/*.jar app.jar EXPOSE 8761 ENTRYPO ...

  2. 一文总结之Redis

    目录 Redis 目标 Redis简介 什么是Redis 特性 Redis当前应用情况 安装 基本使用 键 exists判断键存在性.del删除键.type键类型 expire key的时效性设置 基 ...

  3. 在tensorflow环境下安装matplotlib

    在运行程序时,报错ImportError: No module named 'matplotlib',如图.经网上查询发现是没有安装matplotlib 因此记录一下在tensorflow环境下安装m ...

  4. Python之路3【第零篇】目录

    Web应用框架篇 1.Web应用框架前戏

  5. P2234 [HNOI2002]营业额统计

    题目描述 Tiger最近被公司升任为营业部经理,他上任后接受公司交给的第一项任务便是统计并分析公司成立以来的营业情况. Tiger拿出了公司的账本,账本上记录了公司成立以来每天的营业额.分析营业情况是 ...

  6. Round 1 Over

    终于把题目清单上的 \(dp\) 写完了\(hhh\)

  7. Webpack + React 开发 02 JSX 语法

    HTML 语言直接写在 JavaScript 语言之中,不加任何引号,这就是 JSX 的语法,它允许 HTML 与 JavaScript 的混写: render(<h1>Hello Wor ...

  8. 图解HTTP(六)HTTP首部

    一.HTTP报文的结构: 二.4种首部字段: 1. 通用首部字段 请求报文和响应报文都会使用的首部. 首部字段名 说明 Cache-Control 控制缓存行为 Connection 逐跳首部.连接的 ...

  9. CH3101 阶乘分解

    题目链接 分解\(n!\)的质因数,输出相应的\(p_i\)和\(c_i\). 其中\(1\leq n\leq 10^6\).   考虑每一个质因子 \(p\) 在 \(n!\) 中出现的次数.显然, ...

  10. VS生成事件执行XCOPY时出现Invalid num of parameters的解决方案

    最近想偷懒 想把项目生成的dll全部自动汇集到一个文件夹下 于是乎就动用了VS的生成后事件 在执行Xcopy的时候碰到了点问题 Invalid number of parameters 挺奇怪的,在公 ...