257. Binary Tree Paths
Given a binary tree, return all root-to-leaf paths.
Note: A leaf is a node with no children.
Example:
Input:
1
/ \
2 3
\
5
Output: ["1->2->5", "1->3"]
Explanation: All root-to-leaf paths are: 1->2->5, 1->3
题目链接:https://leetcode-cn.com/problems/binary-tree-paths/
思路
树遍历。
法一:非递归
用queue储存单条路径。
BFS或DFS都可,BFS用queue储存多条路径,DFS用stack。两种方法没差,这里写的是用DFS的方法。
!注意小坑:判定一条路径结束,一定要用判断当前节点是否是叶节点的方法,而不能只是遍历到空孩子就输出路径,此时的路径并不一定走到了叶节点。样例:[1,2,3,null,5]
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
vector<string> binaryTreePaths(TreeNode* root) {
vector<string> res;
if(!root) return res;
stack<queue<TreeNode*>> record;
queue<TreeNode*> q;
q.push(root);
record.push(q);
while(!record.empty()){
auto tmp = record.top();
record.pop();
auto last = tmp.back();
if(!last->left && !last->right){
res.push_back(que2str(tmp));
}
else{
if(last->right){
auto qr = tmp;
qr.push(last->right);
record.push(qr);
}
if(last->left){
tmp.push(last->left);
record.push(tmp);
}
}
}
return res;
}
string que2str(queue<TreeNode*> que){
if(que.empty()) return "";
string res = to_string(que.front()->val);
que.pop();
while(!que.empty()){
res = res + "->" + to_string(que.front()->val);
que.pop();
}
return res;
}
};
改变存储方式:把路径和节点拆成两个存储空间,内存消耗降低,运行时间增加。
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
vector<string> binaryTreePaths(TreeNode* root) {
vector<string> res;
if(!root) return res;
stack<TreeNode*> q;
q.push(root);
stack<string> qs;
string s = to_string(root->val);
qs.push(s);
while(!q.empty()){
auto last = q.top();
q.pop();
string tmp = qs.top();
qs.pop();
if(!last->left && !last->right){
res.push_back(tmp);
}
else{
if(last->left){
string sl = tmp + "->" + to_string(last->left->val);
qs.push(sl);
q.push(last->left);
}
if(last->right){
tmp += "->" + to_string(last->right->val);
qs.push(tmp);
q.push(last->right);
}
}
}
return res;
}
};
法二:递归
逻辑和非递归差不多。
2个编程注意点:
1)输出不是单一答案,而是一组答案的递归方法,可以像此题的参数传递记录答案。
2)传入的需要修改的参数变量,一定得是引用或指针,切记。
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
vector<string> binaryTreePaths(TreeNode* root) {
vector<string> res;
if(!root) return res;
queue<TreeNode*> q;
q.push(root);
getPaths(q, res);
return res;
}
void getPaths(queue<TreeNode*>path, vector<string> &res){
if(path.empty()) return;
auto last = path.back();
if(!last->left && !last->right){
res.push_back(que2str(path));
}else{
if(last->left){
auto pl = path;
pl.push(last->left);
getPaths(pl, res);
}
if(last->right){
path.push(last->right);
getPaths(path, res);
}
}
}
string que2str(queue<TreeNode*> que){
if(que.empty()) return "";
string res = to_string(que.front()->val);
que.pop();
while(!que.empty()){
res = res + "->" + to_string(que.front()->val);
que.pop();
}
return res;
}
};
优化:省去queue
非递归方法中,整个循环的变量都存储在同一个函数栈中,因此每次遍历一个新节点时,需要有人记录此节点是属于众多路径中的哪一条。要么需要两个一样的记录遍历的空间——一个记录待遍历节点(TreeNode*),一个记录此节点目前为止的路径(string);要么就是用queue把两个记录空间合并成一个(queue<TreeNode*>)。
而递归方法中,每一次遍历新节点,它的存储栈是独立于其他人的,因此可以通过传参来告诉其当前节点和所属路径。
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
vector<string> binaryTreePaths(TreeNode* root) {
vector<string> res;
if(!root) return res;
string path = to_string(root->val);
getPaths(path, root, res);
return res;
}
void getPaths(string path, TreeNode* root, vector<string> &res){
if(!root) return;
if(!root->left && !root->right){
res.push_back(path);
}else{
if(root->left){
string sl = path + "->" + to_string(root->left->val);
getPaths(sl, root->left, res);
}
if(root->right){
string sr = path + "->" + to_string(root->right->val);
getPaths(sr, root->right, res);
}
}
}
};
高级
看了分享才理解的,感觉这才是真的熟悉递归的写法。
大概思路:调用递归的运行空间独立,返回值独立,创建的记录结果的空间也是新开的独立的,完全利用这一点,返回的是一条路径上的后半段,那么只要把当前节点和所有后半段连起来,就多向上走了一步,继续往上返回。
这种思想还要继续练习体会。
运行速度秒杀100% c++程序。
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
vector<string> binaryTreePaths(TreeNode* root) {
vector<string> res;
if(!root) return res;
if(!root->left && !root->right){
res.push_back(to_string(root->val));
return res;
}
vector<string> l = binaryTreePaths(root->left);
for(auto item: l){
res.push_back(to_string(root->val) + "->" + item);
}
vector<string> r = binaryTreePaths(root->right);
for(auto item: r){
res.push_back(to_string(root->val) + "->" + item);
}
return res;
}
};