【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 ...
随机推荐
- 109th LeetCode Weekly Contest Number of Recent Calls
Write a class RecentCounter to count recent requests. It has only one method: ping(int t), where t r ...
- PHP验证身份信息
$code = 'code'; $patt = "/^[1-9]\d{5}(19|20)\d{2}((0[1-9])|(10|11|12))([012](\d|(30|31)))\d{3}[ ...
- 关于抓取js加载出来的内容抓取
一.抓取页面 url=https://www.xuexi.cn/f997e76a890b0e5a053c57b19f468436/018d244441062d8916dd472a4c6a0a0b.ht ...
- RMQ、POJ3264
这里说几篇博客,建议从上到下看 https://blog.csdn.net/qq_31759205/article/details/75008659 https://blog.csdn.net/sgh ...
- 深度优先搜索(DFS) — 20180926
用DFS的场景: 找出所有方案:DFS找出所有方案总数 可能是动态规划 DFS时间复杂度:答案个数*构造每个答案的时间 动态规划时间复杂度:状态个数*计算每个状态时间 二叉树时间复杂度:节点数*处理每 ...
- C++ GUI Qt4编程(02)-1.2quit
1. 根据C++ GUI Qt4编程(第二版)整理2. 系统:centos7: Qt版本:5.5.13. 程序:quit.cpp #include <QApplication> #inc ...
- Linux kvm虚拟机的基本操作命令
Linux 虚拟化kvm virsh常用命令篇 1.创建虚拟机 virsh define damo.xml //创建的虚拟机不是活动的 virsh create damo.xml //创建的虚拟机是活 ...
- 剑指offer——面试题4:二维数组中的查找
// 面试题4:二维数组中的查找 // 题目:在一个二维数组中,每一行都按照从左到右递增的顺序排序,每一列都按 // 照从上到下递增的顺序排序.请完成一个函数,输入这样的一个二维数组和一个 // 整数 ...
- pandas to_excel、to_csv的float_format参数设置
df.to_excel(outpath,float_format='%.2f')
- 【Tensorflow】 Object_detection之模型训练日志结果解析
日志展示 指标说明: AP值表示正确识别物体的个数占总识别出的物体个数的百分数 AR值表示正确识别物体的个数占测试集中物体的总个数的百分数 IoU值即生成的框/掩膜与数据集中的标准的面积之交处于面积之 ...