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 / \
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 / \的更多相关文章
- 剑指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 ...
- [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 ...
- [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 ...
- [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 ...
- [LeetCode] Binary Tree Level Order Traversal 二叉树层序遍历
Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, ...
- 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 ...
- [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, ...
- LeetCode - Binary Tree Level Order Traversal II
题目: Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from ...
- 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, ...
随机推荐
- Ionic之$scope 依赖注入报错
在开发Ionic过程中,发现会报在 LoginController 中引用locals报错,具体报错问题: ionic.bundle.js:19526 Error: [$injector:unpr] ...
- LN : Eden Polymorphic And OOP Design Pattern Abstract Factory
Appreciation to our TA, +7, who designed this task. Client.cpp #include <iostream> #include &l ...
- CF1081C Colorful Bricks
思路: dp[i][j]表示到第i个砖块为止共计有j个砖块和它左边的砖块颜色不同. 实现: #include <bits/stdc++.h> using namespace std; ty ...
- 洛谷 P2935 [USACO09JAN]最好的地方Best Spot
题目描述 Bessie, always wishing to optimize her life, has realized that she really enjoys visiting F (1 ...
- (十一)maven之安装nexus私服
安装nexus私服 前面的文章中对项目引入jar依赖包的时候,maven一般先是在本地仓库找对应版本的jar依赖包,如果在本地仓库中找不到,就上中央仓库中下载到本地仓库. 然而maven默认提供的中央 ...
- 通过90行代码学会HTML5 WebSQL的4种基本操作
Web SQL数据库API是一个独立的规范,在浏览器层面提供了本地对结构化数据的存储,已经被很多现代浏览器支持了. 我们通过一个简单的例子来了解下如何使用Web SQL API在浏览器端创建数据库表并 ...
- JAVA初级必须要搞懂的事项(希望对新手有所帮助)
1 安装JDK=> (1,下载JDK,安装,一般目录为C:\Program Files\Java中:2,通过Dos命令测试JDK是否安装=>java –version命令查看 ...
- 怎样将英文版的Eclipse转为中文版的?
=====>1.打开eclipse储存文件夹 =====>2.在eclipse文件中找到dropins文件 =====>3.把已经下载好的eclipse汉化包复制到dropins中 ...
- CAD交互绘制圆形批注(网页版)
js中实现代码说明: 动态拖放时的绘制事件: function DoDynWorldDrawFun(dX,dY,pWorldDraw,pData) { //自定义实体的GUID标识符 var sGui ...
- 在 webpack 中使用 ECharts
http://echarts.baidu.com/tutorial.html#%E5%9C%A8%20webpack%20%E4%B8%AD%E4%BD%BF%E7%94%A8%20ECharts W ...