257. 二叉树的所有路径 - 力扣(LeetCode)
题目描述
给你一个二叉树的根节点 root
,按 任意顺序 ,返回所有从根节点到叶子节点的路径。
叶子节点 是指没有子节点的节点。
样例输入
示例 1:
输入:root = [1,2,3,null,5] 输出:["1->2->5","1->3"]
示例 2:
输入:root = [1] 输出:["1"]
提示:
- 树中节点的数目在范围
[1, 100]
内 -100 <= Node.val <= 100
代码
前序递归
版本一
/**
* Definition for a binary tree node.
* 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 {
private:
vector<int> path;//收集中间结果
vector<string> res;//收集所有结果
public:
void backing(TreeNode* root)
{
path.push_back(root->val);//前序遍历
//遇到叶子结点就收集结果
if(!root->left && !root->right)
{
//拼接字符串
string strPath;
for(int i=0;i<path.size()-1;i++)
{
strPath+=to_string(path[i]);
strPath+="->";
}
strPath+=to_string(path[path.size()-1]);
//收集结果
res.push_back(strPath);
return ;
}
//递归遍历左子树
if(root->left){
backing(root->left);
path.pop_back();//回溯
}
//递归遍历右子树
if(root->right){
backing(root->right);
path.pop_back();//回溯
}
}
vector<string> binaryTreePaths(TreeNode* root) {
backing(root);
return res;
}
};
版本二
/**
* Definition for a binary tree node.
* 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 {
private:
vector<string> res;
public:
//此处path不能使用引用类型,
//因为值传递的复制效果,使得函数在返回上一层时间接实现了递归的回溯
void backing(TreeNode* root,string path)
{
path+=to_string(root->val);
if(!root->left && !root->right)
{
res.push_back(path);
}
if(root->left) backing(root->left,path+"->");
if(root->right) backing(root->right,path+"->");
}
vector<string> binaryTreePaths(TreeNode* root) {
if(!root) return res;
string path;
backing(root,path);
return res;
}
};
版本三
版本二的代码拆开后等价于以下代码
/**
* Definition for a binary tree node.
* 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 {
private:
vector<string> res;
public:
//此处path不能使用引用类型,
//因为值传递的复制效果,使得函数在返回上一层时间接实现了递归的回溯
void backing(TreeNode* root,string path)
{
path+=to_string(root->val);
if(!root->left && !root->right)
{
res.push_back(path);
return ;
}
//其实现效果与版本二中的算法等同
if(root->left) {
path+="->";//这是本层
backing(root->left,path);//这是下一层
path.pop_back();//回溯本层的‘>’
path.pop_back();//回溯本层的‘-’
}
if(root->right){
path+="->";
backing(root->right,path);
path.pop_back();
path.pop_back();
}
}
vector<string> binaryTreePaths(TreeNode* root) {
if(!root) return res;
string path;
backing(root,path);
return res;
}
};
前序非递归
/**
* Definition for a binary tree node.
* 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> res;
if(!root) return res;
stack<TreeNode*> pre;
stack<string> path;
pre.push(root);
path.push(to_string(root->val));
while(!pre.empty())
{
TreeNode* p=pre.top();pre.pop();
string strPath=path.top();path.pop();
if(!p->left && !p->right)
{
res.push_back(strPath);
}
if(p->right)
{
pre.push(p->right);
path.push(strPath+"->"+to_string(p->right->val));
}
if(p->left)
{
pre.push(p->left);
path.push(strPath+"->"+to_string(p->left->val));
}
}
return res;
}
};