95、【树与二叉树】leetcode ——257. 二叉树的所有路径:递归法DFS/回溯法+迭代法DFS+层序遍历BFS(C++版本)

题目描述

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
原题链接:257. 二叉树的所有路径

解题思路

一、递归法

(1) 先序遍历DFS

/**
 * 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> res;
    void traversal(string s, TreeNode* root) {        
        if(!root->left && !root->right) {            
            s += to_string(root->val);          // 对叶子处和非叶子处的字符串相加分别处理
            res.push_back(s);
            return ;
        }           
        s += to_string(root->val) + "->";       // 非叶子后面带上箭头,叶子后面不带箭头
        if(root->left)      traversal(s, root->left);
        if(root->right)     traversal(s, root->right);
    }
    vector<string> binaryTreePaths(TreeNode* root) {
        traversal("", root);             
        return res;
    }
};

时间复杂度 O ( n 2 ) O(n^2) O(n2)
空间复杂度 O ( n 2 ) O(n^2) O(n2)

(2)标准回溯法形式

/**
 * 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:
    void traversal(TreeNode* root, vector<int>& path, vector<string>& res) {
        path.push_back(root->val);
        // 遍历到根节点,输出结果
        if(!root->left && !root->right) {
            string s = "";
            for(int i = 0; i < path.size() - 1; i++) {
                s += to_string(path[i]);
                s += "->";
            }
            // 左后一个单独相加
            s += to_string(path[path.size() - 1]);
            res.push_back(s);
            return ;
        }
        // 左子树不为空,遍历,遍历完后回退
        if(root->left) {
            traversal(root->left, path, res);
            path.pop_back();
        }
        // 左子树不为空,遍历,遍历完后回退
        if(root->right) {
            traversal(root->right, path, res);
            path.pop_back();
        }
    }
    vector<string> binaryTreePaths(TreeNode* root) {
        vector<int> path;
        vector<string> res;
        traversal(root, path, res);
        return res;
    }
};

时间复杂度 O ( n 2 ) O(n^2) O(n2)
空间复杂度 O ( n 2 ) O(n^2) O(n2)

二、迭代法

(1)先序遍历

/**
 * 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;        
        stack<TreeNode*> st;        
        stack<string> path;         // 先序需要使用栈,为保持顺序一致,也用栈维护每条路径上的
        st.push(root);
        path.push(to_string(root->val));
        while(!st.empty()) {
            TreeNode* node = st.top();      st.pop();
            string s = path.top();          path.pop();
            if(!node->left && !node->right) {
                res.push_back(s);
            }
            // 因为栈是先进后出,因此先压入右,再压入左,弹出时候才会是先序遍历
            if(node->right) {
                st.push(node->left);
                path.push(s + "->" + to_string(node->left->val));
            }
            if(node->left) {
                st.push(node->right);
                path.push(s + "->" + to_string(node->right->val));
            }
        }
        return res;
    }
};

时间复杂度 O ( n 2 ) O(n^2) O(n2)
空间复杂度 O ( n 2 ) O(n^2) O(n2)

(2)层次遍历BFS

/**
 * 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;
        queue<string> path;         // 层序需要用队列,为保持顺序一致,也用队列存路径
        queue<TreeNode*> que;
        que.push(root);
        path.push(to_string(root->val));
        while(!que.empty()) {
            TreeNode* node = que.front();       que.pop();
            string s = path.front();            path.pop();                
            if(!node->left && !node->right) {
                res.push_back(s);
            }
            if(node->left) {
                que.push(node->left);
                path.push(s + "->" + to_string(node->left->val));
            }
            if(node->right) {
                que.push(node->right);
                path.push(s + "->" + to_string(node->right->val));
            }
        }
        return res;
    }
};

时间复杂度 O ( n 2 ) O(n^2) O(n2)
空间复杂度 O ( n 2 ) O(n^2) O(n2)

参考文章:二叉树的所有路径257. 二叉树的所有路径

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

辰阳星宇

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值