当前位置:网站首页>All paths of 344 leetcode binary tree
All paths of 344 leetcode binary tree
2022-04-22 21:08:00 【sp_ thirteen billion two hundred and thirty million four hundre】

Algorithm : We can also use breadth first search to achieve . We maintain a queue , Storage node and the path from the root to the node . At first, there was only the root node in the queue . In each iteration , We take out the first node in the queue , If it's a leaf node , Then add its corresponding path to the answer . If it's not a leaf node , Then all its child nodes are added to the end of the queue . When the queue is empty, the breadth first search ends , We can get the answer
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;
}
Time complexity :O(N ^ 2), among N Indicates the number of nodes . In depth first search, each node will be accessed once and only once , Every time, it will be right path Variable to copy and construct , The cost of time is O(N), So the time complexity is O(N^2)
Spatial complexity :O(N ^ 2), among N Indicates the number of nodes . In the worst case , There will be in the queue N Nodes , The maximum length of each node in the queue where the string is saved is N, So the complexity of space is O(N^2)
版权声明
本文为[sp_ thirteen billion two hundred and thirty million four hundre]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204222100466376.html
边栏推荐
- 浅学 “等待唤醒机制 ”
- Confidence interval and interval estimation
- 【MySQL从入门到精通】:关于常用like子句中通配符的总结
- 2023 Huazhong University of science and technology automation postgraduate entrance examination ashore experience guidance of predecessors
- Introduction to dynamic programming of leetcode learning plan day1,2 (4 questions in total)
- Count the number of characters C
- 重返天梯-L2-025 分而治之 (25 分)
- 站长工具大全-站长软件网站-站长工具网站-站长必备工具免费
- 农村没网络怎样安监控,家里没有wifi安哪种监控器
- [NOIP2012]借教室
猜你喜欢

Qtp11 tutorial

一、机器学习概念

The interviewer would rather have my younger brother who has just graduated and worked for one year than me who has worked for five years, with an annual salary of 25W

kubernetes_ How to solve the problem that namespace cannot be deleted

(一)UART子系统学习计划

2、 Linear regression
Ali Interviewer: you'd better not write glide on your resume. It's not as simple as asking for the source code

QT使用windeployqt.exe打包程序
![Solution Sudoku [pre DS hash structure + component DFS | pre-hash-1-9 feature -- binary state compression + DFS]](/img/06/295b26f1389da3d90ad211dc447123.png)
Solution Sudoku [pre DS hash structure + component DFS | pre-hash-1-9 feature -- binary state compression + DFS]

MySql指定字段排序、自定义排序位置
随机推荐
东吴证券X袋鼠云:数据轻松可取、毫秒级反应能力,东吴证券做对了什么?
Count the number of characters C
QTP11. 5 / UFT including Chinese package
博途PLC用户自定义数据类型
[play Lighthouse] build WooCommerce store, enable Alipay to pay in person.
buuctf-[Flask]SSTI
2022 electrician (elementary) examination question bank and online simulation examination
OpenVX-将Image文件[pgm格式]读写为vx_image对象,以及写操作
软件测试工具最全的抓包工具的综合对比
联想电脑管家图文介绍:联想电脑管家怎么下载?
Win10 installation neo4j
2022年R2移动式压力容器充装培训试题及在线模拟考试
Solution Sudoku [pre DS hash structure + component DFS | pre-hash-1-9 feature -- binary state compression + DFS]
[200 opencv routines of youcans] 160 Otsu method of image processing
[NOIP2012]借教室
Introduction to unityshader - sketch effect rendering
leetcode-92-反转链表
MySQL advanced stored procedure storage function -- Introduction to stored procedure, basic syntax of stored procedure, variables (system variables, user-defined variables, local variables), if, param
344-Leetcode 二叉树的所有路径
Lenovo computer housekeeper graphic introduction: how to download Lenovo computer housekeeper?