【Leetcode】【Easy】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]
]
BFS解题思路:
使用二叉树按层遍历的方法,很容易解决。
/**
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
vector<vector<int> > levelOrder(TreeNode *root) {
vector<vector<int> > levelOrderLst;
vector<int> valLst;
queue<TreeNode *> curLevelNodes; if (!root)
return levelOrderLst; curLevelNodes.push(root); while (!curLevelNodes.empty()) {
int len = curLevelNodes.size();
while (len--) {
valLst.push_back(curLevelNodes.front()->val); if (curLevelNodes.front()->left) {
curLevelNodes.push(curLevelNodes.front()->left);
} if (curLevelNodes.front()->right) {
curLevelNodes.push(curLevelNodes.front()->right);
} curLevelNodes.pop();
} levelOrderLst.push_back(valLst);
valLst.clear();
} return levelOrderLst; } };
DFS解题思路:
[ [
[3], [3],
[9], ==> [9,20],
[15] [15,7]
] ]
树中,第一层val值保存在vec[0],第N层val值保存在vec[n-1]。因此按DFS原则,可以统一使用“先左后右”的规律,先遍历左子树,再遍历右子树。遍历到的val值插入对应dep的位置中。
注意:将val插入对应dep位置前,此位置上需要存在子列表,才能执行插入命令(代码中10-11行)。
class Solution {
private:
vector<vector<int> > levelOrderLst;
public:
void buildVector(TreeNode *root, int depth)
{
if (root == NULL)
return; if (levelOrderLst.size() == depth)
levelOrderLst.push_back(vector<int>()); levelOrderLst[depth].push_back(root->val); buildVector(root->left, depth + );
buildVector(root->right, depth + );
} vector<vector<int> > levelOrder(TreeNode *root) {
buildVector(root, );
return levelOrderLst;
}
};
附录:
DFS/BFS
queue\vector
【Leetcode】【Easy】Binary Tree Level Order Traversal的更多相关文章
- Leetcode PHP题解--D125 107. Binary Tree Level Order Traversal II
val = $value; } * } */ class Solution { private $vals = []; /** * @param TreeNode $root * @return In ...
- 【Leetcode】【Easy】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
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 来源: htt ...
- 【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】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 ...
- 【Binary Tree Level Order Traversal II 】cpp
题目: Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from ...
- 2016.6.24——vector<vector<int>>【Binary Tree Level Order Traversal】
Binary Tree Level Order Traversal 本题收获: 1.vector<vector<int>>的用法 vector<vector<int ...
- 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(32)-Binary Tree Level Order Traversal
题目: LeetCode Premium Subscription Problems Pick One Mock Articles Discuss Book fengsehng 102. Binary ...
随机推荐
- 【算法笔记】B1037 在霍格沃茨找零钱
1037 在霍格沃茨找零钱 (20 分) 如果你是哈利·波特迷,你会知道魔法世界有它自己的货币系统 —— 就如海格告诉哈利的:“十七个银西可(Sickle)兑一个加隆(Galleon),二十九个纳特( ...
- HDU_6298 Maximum Multiple 【找规律】
一.题目 Given an integer $n$, Chiaki would like to find three positive integers $x$, $y$ and $z$ such t ...
- Codeforces - 617E 年轻人的第一道莫队·改
题意:给出\(n,m,k,a[1...n]\),对于每次询问,求\([l,r]\)中\(a[i] \ xor \ a[i+1] \ xor \ ...a[j],l<=i<=j<=r\ ...
- HDU - 1907 anti-SG
题意:nim游戏,最后取光为[输] anti-SG的应用,搬运一下我的摸鱼小笔记 最先看到的应该是分奇偶的非充裕堆判断,若为偶数则先手胜,否则后手胜 按SG分类 SG!=0时 1.只有一堆大于1,先手 ...
- Tomcat启动分析
[转自] http://docs.huihoo.com/apache/tomcat/heavyz/01-startup.html 1 - Tomcat Server的组成部分 1.1 - Server ...
- scrapy_redis之官网列子domz
一. domz.py from scrapy.linkextractors import LinkExtractor from scrapy.spiders import CrawlSpider, ...
- PIE SDK栅格拉伸控制
1. 功能简介 在我们的实际应用中,对于一般16bit或者更大比特深度的影像,像元值都是大于255的.这种情况下,RGB的显示器是不能够直接使用像元值进行显示的,需要将像元值换算到0~255的区间内以 ...
- Docker 命令详解(run篇)
参考:https://www.cnblogs.com/yfalcon/p/9044246.html 命令格式:docker run [OPTIONS] IMAGE [COMMAND] [ARG...] ...
- (转)模块readline解析
模块readline解析 原文:https://www.cnblogs.com/fireflow/p/4841413.html readline模块定义了一系列函数用来读写Python解释器中历史命令 ...
- flume 自定义sink
http://flume.apache.org/FlumeDeveloperGuide.html#sink 看了 还是比较好上手的,简单翻译一下 sink的作用是从 Channel 提取 Event ...