Binary Tree Level Order Traversal [LeetCode]
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]
]
Solution:
vector<vector<int> > levelOrder(TreeNode *root) {
vector<vector<int> > nums;
if(root == NULL)
return nums;
vector<TreeNode *> level;
level.push_back(root); while(true){
if(level.size() == )
break;
vector<int> level_num;
vector<TreeNode *> new_level;
for(auto item : level) {
level_num.push_back(item->val);
if(item->left != NULL)
new_level.push_back(item->left);
if(item->right != NULL)
new_level.push_back(item->right);
}
nums.push_back(level_num);
level = new_level;
}
return nums;
}
Binary Tree Level Order Traversal [LeetCode]的更多相关文章
- Binary Tree Level Order Traversal - leetcode - java
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 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
LeetCode:Binary Tree Level Order Traversal Given a binary tree, return the level order traversal of ...
- 【一天一道LeetCode】#107. Binary Tree Level Order Traversal II
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 来源: htt ...
- LeetCode(32)-Binary Tree Level Order Traversal
题目: LeetCode Premium Subscription Problems Pick One Mock Articles Discuss Book fengsehng 102. Binary ...
- [leetcode]Binary Tree Level Order Traversal II @ Python
原题地址:http://oj.leetcode.com/problems/binary-tree-level-order-traversal-ii/ 题意: Given a binary tree, ...
- Java for 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(107) Binary Tree Level Order Traversal II
题目 Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from l ...
随机推荐
- 【转载】COM多线程原理与应用
原文:COM多线程原理与应用 目录: COM多线程原理与应用 目录: 前言: 套间: 套间的定义: 套间的分类: 套间的进入和退出: 对象的同步: 组件对象的同步: COM对象线程模型: 进程内对象线 ...
- 【转载】在Linux中使用VS Code编译调试C++项目
原文:在Linux中使用VS Code编译调试C++项目 最近项目需求,需要在Linux下开发C++相关项目,经过一番摸索,简单总结了一下如何通过VS Code进行编译调试的一些注意事项. 关于VS ...
- 数独Sudoku
数独(すうどく,Sūdoku),是源自18世纪瑞士发明,流传到美国,再由日本发扬光大的一种数学游戏.是一种运用纸.笔进行演算的逻辑游戏.玩家需要根据9×9盘面上的已知数字,推理出所有剩余空格的数字,并 ...
- Using GET_GROUP_SELECTION For Record Groups in Oracle Forms
Retrieves the sequence number of the selected row for the given group. Suppose you want to get a par ...
- How To Tune or Test PLSQL Code Performance in Oracle D2k Forms
You can test or tune your program unit performance in Oracle forms with Ora_Prof package.Suppose you ...
- IIS网站部署注意点
在IIS上部署网站时,除了在添加网站时配置好相关程序池,主目录,安全性,选择.Netframwork版本这些步骤外, 容易忘记的是有些网站需要打开web服务扩展.
- .NET/C# 使用Stopwatch测量运行时间
Stopwatch类:http://msdn.microsoft.com/zh-cn/library/system.diagnostics.stopwatch(v=vs.100).aspx 常用属性和 ...
- iOS - OC iOS 开发体系
1.iOS 开发技术体系 iOS 开发技术体系图: 层级 主要框架 Cocoa Touch UIKit 等 Media Core Graphics .OpenGl ES.Core Animation ...
- 高并发简单解决方案————redis队列缓存+mysql 批量入库(ThinkPhP)
问题分析 问题一:要求日志最好入库:但是,直接入库mysql确实扛不住,批量入库没有问题,done.[批量入库和直接入库性能差异] 问题二:批量入库就需要有高并发的消息队列,决定采用redis lis ...
- jqurey click和blur执行时间冲突
参考资料:http://stackoverflow.com/questions/10652852/jquery-fire-click-before-blur-event