给你一个二叉树的根节点 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:
void traversal(TreeNode* cur,vector<int>&path,vector<string>&result){
//把中放入
path.push_back(cur->val);
//当指向叶子节点时
if(cur->left==NULL&&cur->right==NULL){
string Spath;
for(int i=0;i<path.size()-1;i++){
Spath+=to_string(path[i]);
Spath+="->";
}
//叶子节点单独转换,因为需要遍历到叶子节点马上存放路径
Spath+=to_string(path[path.size()-1]);
//将该条路径存放在大容器中
result.push_back(Spath);
return;
}
if(cur->left){
traversal(cur->left,path,result);
path.pop_back();//回溯
}
if(cur->right){
traversal(cur->right,path,result);
path.pop_back();//回溯
}
}
public:
vector<string> binaryTreePaths(TreeNode* root) {
//定义一个大容器,存放所有的字符串(路径)
vector<string>result;
//定义一个小容器,遍历完一边的路径即可清理
vector<int>path;
//判断根节点是否为空,为空即返回
if(root==NULL)return result;
//递归遍历二叉树
traversal(root,path,result);
return result;
}
};
递归法(化简版):
/**
* 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:
void traversal(TreeNode* node,string path,vector<string>&result){
path+=to_string(node->val);
if(node->left==NULL&&node->right==NULL){
result.push_back(path);
return;
}
if(node->left){
traversal(node->left,path+"->",result);
}
if(node->right){
traversal(node->right,path+"->",result);
}
}
public:
vector<string> binaryTreePaths(TreeNode* root) {
vector<string>result;
string path;
if(root==NULL){
return result;
}
traversal(root,path,result);
return result;
}
};
迭代法(后序):
/**
* 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) {
//保存遍历路径的节点
stack<string>pathSt;
//保存树的遍历节点
stack<TreeNode*>treeSt;
//保存最终结果
vector<string>result;
//如果根节点指向为空,直接返回
if(root==NULL)return result;
//遍历节点和遍历路径的节点放入栈中
treeSt.push(root);
pathSt.push(to_string(root->val));
//遍历二叉树
while(!treeSt.empty()){
//取出节点,中
TreeNode* node=treeSt.top();treeSt.pop();
//取出该节点对应的路径
string path=pathSt.top();pathSt.pop();
//遇到叶子节点
if(node->left==NULL&&node->right==NULL){
result.push_back(path);
}
//右
if(node->right){
treeSt.push(node->right);
pathSt.push(path+"->"+to_string(node->right->val));
}
//左
if(node->left){
treeSt.push(node->left);
pathSt.push(path+"->"+to_string(node->left->val));
}
}
return result;
}
};