力扣刷题(day0035)二叉树的所有路径

给你一个二叉树的根节点 root ,按 任意顺序 ,返回所有从根节点到叶子节点的路径。

叶子节点 是指没有子节点的节点。

示例 1:

力扣刷题(day0035)二叉树的所有路径_第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&path,vector&result){
        //把中放入
        path.push_back(cur->val);
        //当指向叶子节点时
        if(cur->left==NULL&&cur->right==NULL){
            string Spath;
            for(int i=0;ileft){
            traversal(cur->left,path,result);
            path.pop_back();//回溯
        }
        if(cur->right){
            traversal(cur->right,path,result);
            path.pop_back();//回溯
        }
    }
public:
    vector binaryTreePaths(TreeNode* root) {
        //定义一个大容器,存放所有的字符串(路径)
        vectorresult;
        //定义一个小容器,遍历完一边的路径即可清理
        vectorpath;
        //判断根节点是否为空,为空即返回
        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&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 binaryTreePaths(TreeNode* root) {
    vectorresult;
    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 binaryTreePaths(TreeNode* root) {
        //保存遍历路径的节点
        stackpathSt;
        //保存树的遍历节点
        stacktreeSt;
        //保存最终结果
        vectorresult;
        //如果根节点指向为空,直接返回
        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;
    }
};

你可能感兴趣的:(算法,数据结构,leetcode)