【Lintcode】069.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).
Given binary tree {3,9,20,#,#,15,7},
3
/ \
9 20
/ \
15 7
return its level order traversal as:
[
[3],
[9,20],
[15,7]
]
题解:
三种处理方法:queue + queue; queue + ‘dummy’;queue
Solution 1 ()
class Solution {
/**
* @param root: The root of binary tree.
* @return: Level order a list of lists of integer
*/
public:
vector<vector<int>> levelOrder(TreeNode *root) {
vector<vector<int>> res;
if (root == NULL) {
return res;
}
queue<TreeNode*> queue;
vector<int> levelNode;
queue.push(root);
queue.push(nullptr);
while(!queue.empty()) {
TreeNode *node = queue.front();
queue.pop();
if (node == nullptr) {
res.push_back(levelNode);
levelNode.resize();
if (q.size() > ) {
q.push(nullptr);
}
} else {
levelNode.push_back(root->val);
if (node->left != nullptr) {
queue.push(node->left);
}
if (node->right != nullptr) {
queue.push(node->right);
}
}
}
return res;
}
};
Solution 2 ()
class Solution {
/**
* @param root: The root of binary tree.
* @return: Level order a list of lists of integer
*/
public:
vector<vector<int>> levelOrder(TreeNode *root) {
vector<vector<int>> res;
if (root == NULL) {
return res;
}
queue<TreeNode*> queue;
queue.push(root);
while(!queue.empty()) {
int size = queue.size();
vector<int> levelNode;
for (int i = ; i < size; i++) {
TreeNode *node = queue.front();
queue.pop();
levelNode.push_back(node->val);
if (node->left != nullptr) {
queue.push(node->left);
}
if (node->right != nullptr) {
queue.push(node->right);
}
}
res.push_back(levelNode);
}
return res;
}
};
程序中的size是必要的,因为在for循环中queue是不断变化的,那么queue.size()也是不断变化的,这就违背了我们的原则,这个size就是该层所有的非空node的个数,取完之后就要压入result中,读取下一层的节点
【Lintcode】069.Binary Tree Level Order Traversal的更多相关文章
- 【Lintcode】070.Binary Tree Level Order Traversal II
题目: Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from ...
- 【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 ...
- 【LeetCode】102. Binary Tree Level Order Traversal (2 solutions)
Binary Tree Level Order Traversal Given a binary tree, return the level order traversal of its nodes ...
- 【LeetCode】107 - 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】102 - Binary Tree Level Order Traversal
Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, ...
- 【LeetCode】107. Binary Tree Level Order Traversal II 解题报告 (Python&C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:DFS 方法二:迭代 日期 [LeetCode ...
- 【LeetCode】102. Binary Tree Level Order Traversal 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 DFS BFS 日期 题目描述 Given a bi ...
- 【LeetCode】102. Binary Tree Level Order Traversal 二叉树的层序遍历 (Python&C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 BFS DFS 日期 题目地址:https://lee ...
- 【easy】107. Binary Tree Level Order Traversal II 按层输出二叉树
按层输出二叉树,广度优先. 3 / \ 9 20 / \ 15 7 [ [15,7], [9,20], [3] ] /** * Definition for a binary tree node. * ...
随机推荐
- Spring IOC源码分析之-刷新前的准备工作
目录 ClassPathXmlApplicationContext的注册方式 加载父子容器 配置路径解析 容器刷新 刷新容器之刷新预处理 ClassPathXmlApplicationContext的 ...
- jQuery的Pagenation分页插件。
插件简介 此jQuery插件为Ajax分页插件,一次性加载,故分页切换时无刷新与延迟,如果数据量较大不建议用此方法,因为加载会比较慢. 原插件CSS不太合理,使用浮动,故无法方便实现左右方向的定位,且 ...
- u-boot 学习系列 1 - SPL
u-boot这个东西从自我N年前使用到现在,变化好多,今天开始重新研究下,本系列的研究都是基于BeagleBoneBlack(bbb)开发板和 u-boot v201801版本的. SPL介绍 在源代 ...
- static修饰内部类
创建内容类的方式通过外部类的实例对象来创建 public class AA { int a =1; class BB { int b=3 ; } public static void main(Str ...
- iOS - 集成SDK问题
1.大部分社交平台接口不支持https协议. 问题描述:在iOS9下,系统默认会拦截对http协议接口的访问,因此无法获取http协议接口的数据.对ShareSDK来说,具体表现可能是,无法授权.分享 ...
- PHP数据类型转换和运算符表达式
一:数据类型的转换 获取类型: gettype($a); 判断是否是某种类型的数据: is_类型名($a); 1.(int)$a; 2.settype($a,int); 二:运算符表达式 1.数学运算 ...
- GS给客户单发包以及m_queGcWait(所有GC共享)
GS给客户单发包以及m_queGcWait(所有GC共享) send_stat BaseChannel::SendCmd(int nCmd, void* pData, int nLen) { Prot ...
- 解决因 gtx 显卡而导致的 google chrome 颜色显示不正常。色彩变淡发白,其实很简单
笔者因为换了用 gtx 1050 显卡替换了原来的集显. 导致chrome浏览器渲染颜色变淡而且泛白. 查了下肯能是因为换了显卡,没换高清显示器. 导致chrome自动启用了 dispaly p3 d ...
- EasyDSS RTMP流媒体服务器的HTTP接口query url的C++实现方法
EasyDSS支持HTTP GET接口访问,我们需要获取url的各种参数信息 比如http://ip:port/action?a=1&b=2&c=3 我们需要知道对应的a.b.c的值 ...
- 常用脚本--查看死锁和阻塞usp_who_lock(转)
USE [master] GO /****** Object: StoredProcedure [dbo].[sp_who_lock] Script Date: 02/07/2014 11:51:24 ...