【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 ...
随机推荐
- Fliptile (dfs+二进制压缩)
Farmer John knows that an intellectually satisfied cow is a happy cow who will give more milk. He ha ...
- openLayers地图缩放的回调
//设置地图最小缩放级别为17级 map.events.register("zoomend", this, function (e) { //每次地图缩放时就会进入到这 if (m ...
- springboot(五)-使用Redis
Redis服务器 springboot要使用redis,首先当然要确保redis服务器能够正常跑起来. pom.xml 这里添加redis的依赖,当然也是springboot集成好的. <!-- ...
- [转] 一个简单的零配置命令行HTTP服务器 - http-server (nodeJs)
[From] http://www.cnblogs.com/lucker/p/4108838.html http-server 是一个简单的零配置命令行HTTP服务器, 基于 nodeJs. 如果你不 ...
- 【研究】struts2-045漏洞
攻击者可以通过构造HTTP请求头中的Content-Type值可能造成远程代码执行. 工具: K8(链接:https://pan.baidu.com/s/1kVxgFNx 密码:ygxf) Tomca ...
- Java 不可变对象
不可变对象(immutable objects):一旦对象被创建,它们的状态就不能被改变(包括基本数据类型的值不能改变,引用类型的变量不能指向其他的对象,引用类型指向的对象的状态也不能改变),每次对他 ...
- PHP房贷计算器代码,等额本息,等额本金
debx(); function debx() { $dkm = 240; //贷款月数,20年就是240个月 $dkTotal = 10000; //贷款总额 $dknl = 0.0515; //贷 ...
- 网站ico那点事儿
一. 如何获取某个网站的favicon.ico http://moco.imooc.com/player/report.html 今天看到这个网站上,左侧的小图片挺好看的,想弄下来,检查源码,也没有看 ...
- spark第十八篇:Tuning Spark 调优
由于大多数Spark应用都是在内存中计算的,所以,Spark程序的瓶颈可能是集群中的任何资源,比如CPU,网络带宽或者内存等.本指南主要涵盖两个主题: 1.数据序列化.这对于良好的网络性能至关重要,还 ...
- XLua 网络加载(基础操作)
LoadGameMethod 网上资源加载更新:加载场景中另建协程用来加载; public void LoadGameMethod() { StartCoroutine(start()); ...