leetcode_question_102 Binary Tree Level Order Traversal
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
/ \
15 7
return its level order traversal as:
[
[3],
[9,20],
[15,7]
]
vector<vector<int> > levelOrder(TreeNode *root) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
vector<vector<int>> matrix;
if(root == NULL) return matrix;
vector<int> r;
r.push_back(root->val);
matrix.push_back(r); vector<TreeNode *> path;
path.push_back(root); int count = 1;
while(!path.empty())
{
TreeNode* tn = path.front();
if(tn->left) path.push_back(tn->left);
if(tn->right) path.push_back(tn->right);
path.erase(path.begin());
count--;
if(count==0)
{
vector<int> tmp;
vector<TreeNode*>::iterator it = path.begin();
for(;it != path.end(); ++it)
tmp.push_back((*it)->val);
if(tmp.size() > 0)
matrix.push_back(tmp);
count = path.size();
}
}
return matrix;
}
leetcode_question_102 Binary Tree Level Order Traversal的更多相关文章
- [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 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 I & II
Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, ...
- LeetCode之Binary Tree Level Order Traversal 层序遍历二叉树
Binary Tree Level Order Traversal 题目描述: Given a binary tree, return the level order traversal of its ...
- 35. Binary Tree Level Order Traversal && Binary Tree Level Order Traversal II
Binary Tree Level Order Traversal OJ: https://oj.leetcode.com/problems/binary-tree-level-order-trave ...
- LeetCode:Binary Tree Level Order Traversal I II
LeetCode:Binary Tree Level Order Traversal Given a binary tree, return the level order traversal of ...
- 【Binary Tree Level Order Traversal II 】cpp
题目: Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from ...
- lettcode-102:Binary Tree Level Order Traversal (Java)
Binary Tree Level Order Traversal 二叉树的层序遍历 两种方式: 1.用两个queue交替表示每一层的节点 2.用两个node,一个表示当前层的最后一个节点,一个表示下 ...
- Binary Tree Level Order Traversal II 解题思路
思路: 与Binary Tree Level Order Traversal I 几乎一样.只是最后将结果存放在栈里,然后在栈里再传给向量即可. 再次总结思路: 两个queue,先把第一个放进q1,循 ...
随机推荐
- poj 1753 Flip Game(bfs状态压缩 或 dfs枚举)
Description Flip game squares. One side of each piece is white and the other one is black and each p ...
- Sybase isql常用命令
isql -Usa -S服务名 --常规登录 isql -Usa -S服务名 -Jcp936 --sa用户在客户端使用字符集cp936登录 C:\Users\Administrator>isql ...
- HA高可用的搭建
HA 即 (high available)高可用,又被叫做双机热备,用于关键性业务. 简单理解就是,有两台机器A和B,正常是A提供服务,B待命闲置,当A宕机或服务宕掉,会切换至B机器继续提供服务.常用 ...
- 关于asp.net简单的下载问题
关于asp.net的下载,只需将打开相应的文件路径就能在浏览器上实现下载功能,比如项目的同级目录上有一个文件 苍老师.zip<a href="苍老师.zip">< ...
- PHP.INI常用设置一览表(持续更新)
在编程的过程中遇到或发现的问题,会持续的更新: 1. 打破var_dump的显示瓶颈 php开发环境里,安装了xdebug模块后,var_dump()输出的结果将比较易于查看,但默认情况下,var_d ...
- CREATE DATABASE建库语句详解
原创地址:http://blog.csdn.net/guguda2008/article/details/5716939 一个完整的建库语句是类似这样的: IF DB_ID('TEST') IS NO ...
- XPath语法 在C#中使用XPath示例
XPath可以快速定位到Xml中的节点或者属性.XPath语法很简单,但是强大够用,它也是使用xslt的基础知识. 示例Xml: <?xml version="1.0" en ...
- (zz)Lambda 表达式(C# 编程指南)
https://msdn.microsoft.com/zh-cn/library/bb397687.aspx Lambda 表达式是一种可用于创建委托或表达式目录树类型的匿名函数.通过使用 lambd ...
- session进程和服务
session如果是存在进程内的话,当进程重启,session数据就会丢失. 所以就找到了个办法,不吧session放在进程内,而是放在服务器上.这样子只要服务器不重启,session数据就不会丢失. ...
- HDU 5794 - A Simple Nim
题意: n堆石子,先拿光就赢,操作分为两种: 1.任意一堆中拿走任意颗石子 2.将任意一堆分成三小堆 ( 每堆至少一颗 ) 分析: 答案为每一堆的 ...