当前位置:网站首页>344-Leetcode 二叉树的所有路径
344-Leetcode 二叉树的所有路径
2022-04-22 21:01:00 【sp_13230409636】

算法:我们也可以用广度优先搜索来实现。我们维护一个队列,存储节点以及根到该节点的路径。一开始这个队列里只有根节点。在每一步迭代中,我们取出队列中的首节点,如果它是叶子节点,则将它对应的路径加入到答案中。如果它不是叶子节点,则将它的所有孩子节点加入到队列的末尾。当队列为空时广度优先搜索结束,我们即能得到答案
struct TreeNode
{
int val;
TreeNode* left;
TreeNode* right;
TreeNode() : val(0), left(nullptr), right(nullptr) {
}
TreeNode(int x) : val(x), left(nullptr), right(nullptr) {
}
TreeNode(int x, TreeNode* left, TreeNode* right) : val(x), left(left), right(right) {
}
};
class Solution
{
public:
vector<string> binaryTreePaths(TreeNode* root)
{
vector<string> paths;
if (root == nullptr) return paths;
queue<TreeNode*> node_queue;
queue<string> path_queue;
node_queue.push(root);
path_queue.push(to_string(root->val));
while (!node_queue.empty())
{
TreeNode* t = node_queue.front(); node_queue.pop();
string path = path_queue.front(); path_queue.pop();
if (t->left == nullptr && t->right == nullptr)
{
paths.push_back(path);
}
else
{
if (t->left != nullptr)
{
node_queue.push(t->left);
path_queue.push(path + "->" + to_string(t->left->val));
}
if (t->right != nullptr)
{
node_queue.push(t->right);
path_queue.push(path + "->" + to_string(t->right->val));
}
}
}
return paths;
}
};
int main()
{
Solution A;
TreeNode* t = new TreeNode(1, new TreeNode(2, nullptr, new TreeNode(5, nullptr, nullptr)), new TreeNode(3, nullptr, nullptr));
vector<string> vs = A.binaryTreePaths(t);
for (auto& x : vs)
{
cout << x << endl;
}
return 0;
}
时间复杂度:O(N ^ 2),其中 N 表示节点数目。在深度优先搜索中每个节点会被访问一次且只会被访问一次,每一次会对 path 变量进行拷贝构造,时间代价为 O(N),故时间复杂度为 O(N^2)
空间复杂度:O(N ^ 2),其中 N 表示节点数目。在最坏情况下,队列中会存在 N 个节点,保存字符串的队列中每个节点的最大长度为 N,故空间复杂度为 O(N^2)
版权声明
本文为[sp_13230409636]所创,转载请带上原文链接,感谢
https://blog.csdn.net/weixin_45964837/article/details/124349359
边栏推荐
- 转载:程序员的发展方向
- [server data recovery] a data recovery case in which multiple hard disks are disconnected and the server crashes due to water ingress in the server
- After working for three years as a programmer in a small 9K factory with a monthly salary, he finally won the byte offer he had been longing for for for for a long time in his spare time. The salary c
- MySQL 进阶 存储过程 存储函数 -- 存储过程介绍、存储过程基本语法、变量(系统变量、用户定义变量、局部变量)、if、参数、case、while、repeat、loop、游标、条件处理程序
- DSPACE模拟简单事故现场
- Implementation of simple FreeRTOS task blocking delay based on gd32f1x0 manual programming
- 产品和服务谁重要,长安福特告诉你“全都要”
- 互联网时代没有囊括进来的流量,在产业互联网时代全部都被囊括进来
- 手写链表~内含单向链表和双向链表,请大家放心食用
- 农村没网络怎样安监控,家里没有wifi安哪种监控器
猜你喜欢

.103Navigator

二、线性回归

nodejs笔记2

软件测试知识点 | Jmeter实现接口关联小结

Programming utilities, there is always one you use

DSPACE仿真平台的使用

Confidence interval and interval estimation

MySQL高可用架构设计分析

Smart agriculture has become a development path, give full play to intelligence and liberate manpower

京东新品情报局剧透OPPO K10系列 或联动国漫IP带来惊喜?
随机推荐
Basic use and principle of Minio
华中师范大学少年儿童组织与思想意识教育考研上岸前辈备考经验
Huawei machine test questions summary
Nodejs notes 2
kubernetes_Namespace无法删除的解决方法
nodejs笔记3
基于OpenGL的贪吃蛇游戏设计与实现
TC process addition status
Ordinary functions as friends (using examples to solve friend functions)
7-1 C language programming experiment 6-3 insertion of one-way linked list (30 points)
阿里面试官:简历上最好不要写Glide,不是问源码那么简单
Ali Interviewer: you'd better not write glide on your resume. It's not as simple as asking for the source code
大量mapper IO优化(使用多线程异步+CountDownLatch)
.102键盘移动div
Reprint: the development direction of programmers
Leetcode question brushing Diary - Sword finger offer II 070 Sort numbers that appear only once in the array
Chapter I Introduction to virtual reality technology
UnityShader入门精要——素描效果渲染
TC fabric manager - packaging and unpacking
2022年G3锅炉水处理国家题库及在线模拟考试