class Solution {
public:
vector<vector<int>> levelOrder(TreeNode* root)
{ vector<vector<int>> res;
if (root == NULL)
{
return res;
}
queue<TreeNode*,int>> q;
q.push(make_pair(root,0));
while(!q.empty()){ TreeNode* node=q.front().first;
int level=q.front().second;
q.pop();
if(level==res.size())
res.push_back(vector<int>());
res[level].push_back(node->val);
if(node->left)
q.push(make_pair(node->left,level+1));
if(node->right)
q.push(make_pair(node->right,level+1));
}
return res;
}
};

  

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,#,#,15,7}, 3 / \ 9 20 / \的更多相关文章

  1. 剑指offer从上往下打印二叉树 、leetcode102. Binary Tree Level Order Traversal(即剑指把二叉树打印成多行、层序打印)、107. Binary Tree Level Order Traversal II 、103. Binary Tree Zigzag Level Order Traversal(剑指之字型打印)

    从上往下打印二叉树这个是不分行的,用一个队列就可以实现 class Solution { public: vector<int> PrintFromTopToBottom(TreeNode ...

  2. [LC] 429. N-ary Tree Level Order Traversal

    Given an n-ary tree, return the level order traversal of its nodes' values. Nary-Tree input serializ ...

  3. [LeetCode] Binary Tree Level Order Traversal II 二叉树层序遍历之二

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

  4. [LeetCode] Binary Tree Zigzag Level Order Traversal 二叉树的之字形层序遍历

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

  5. [LeetCode] Binary Tree Level Order Traversal 二叉树层序遍历

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

  6. Binary Tree Zigzag Level Order Traversal [LeetCode]

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

  7. [Leetcode][JAVA] Binary Tree Level Order Traversal

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

  8. LeetCode - Binary Tree Level Order Traversal II

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

  9. leetcode 102. Binary Tree Level Order Traversal

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

随机推荐

  1. mybatis内置二级缓存。

    一.查询缓存的使用,主要是为了提供查询访问速度.将用户对同一数据的重复查询过程简化, 不再每次均从数据库查询获取结果数据,从而提高访问速度. 二.内置二级缓存... 由于MyBatista从缓存中读取 ...

  2. Android 8.0 NotificationChannel 采坑实例

    Android O 上Notification的新特性: 通知通道功能 1. 简介: 通知通道功能使开发者管理自己应用的通知成为一个组或者一个通道,用户可以通过通知通道完成设置通知,如:阻止所有通知, ...

  3. C# DataTable的詳細用法 (转)

    在项目中经常用到DataTable,如果DataTable使用得当,不仅能使程序简洁实用,而且能够提高性能,达到事半功倍的效果,现对DataTable的使用技巧进行一下总结. 一.DataTable简 ...

  4. JMeter3.2入门使用教程

    JMeter3.2入门使用教程 背景说明 1.1. 背景简介 JMeter是Apache软件基金会下的一个开源项目,纯java开发的应用工具,可以作为进行负载和压力测试的工具来使用.从最开始时被设计成 ...

  5. MySQL常用命令和语句

    1.常用SQL语句 1)常用函数/*type可取值为:MICROSECONDSECONDMINUTEHOURDAYWEEKMONTHQUARTERYEARSECOND_MICROSECONDMINUT ...

  6. iOS 查看包架构信息

    lipo -info libUMSocial_Sdk_4.2.a 查看包架构信息

  7. osx launchpad图标的删除

    安装了个parallels desktop之后,OSX中的launchpad中的图标多了不少,可是好多都不是我自己想要的,我们该怎么删除或者修改呢,下面介绍一些方法: ①直接操作Appications ...

  8. WPF中在后台实现控件样式

    加入现在有一个Button的样式如下: <Style TargetType="{x:Type Button}" x:Key="MyButton">. ...

  9. 使用js将后台返回的数据转换成树形结构

    将类似如下数据转换成树形的数据: [ { id: 1, name: '1', }, { id: 2, name: '1-1', parentId: 1 }, { id: 3, name: '1-1-1 ...

  10. PAT (Basic Level) Practise (中文)-1034. 有理数四则运算(20)

    PAT (Basic Level) Practise (中文)-1034. 有理数四则运算(20)  http://www.patest.cn/contests/pat-b-practise/1034 ...