leetcode 257 二叉树的所有路径

简介: leetcode 257 二叉树的所有路径

二叉树的所有路径


70fa4fe8c6b44a559d74b4a4c475dfde.png

递归法(非回溯,浪费内存)

直接递归字符串,每一次递归都产生一个新字符串。没有回溯的过程,浪费空间。

/**
 * 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> result;
    void get_road(TreeNode* cur  , string &s)
    {
        if(cur == nullptr) return;
        s += to_string(cur->val);
        if(cur->left == nullptr && cur->right == nullptr )
        {
            result.push_back(s);
            return;
        }else if(cur->left != nullptr && cur->right == nullptr )
        {   
            s += "->";
            string s1 =s;
            get_road(cur->left ,s1);
        }else if (cur->left == nullptr && cur->right != nullptr )
        {
            s += "->";
            string s1 =s;
            get_road(cur->right ,s1);
        }else
        {
            s += "->";
            string s1 =s;
            string s2 =s;
            get_road(cur->left ,s1);
            get_road(cur->right ,s2);
        }
        return;
    }
    vector<string> binaryTreePaths(TreeNode* root) {
        string s;
        get_road(root ,s);
        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:
    void get_road(TreeNode* cur  , vector<string> &result , vector<int> &path)
    {
        if(cur == nullptr) return;
        path.push_back(cur->val);
        if(cur->left == nullptr && cur->right == nullptr )
        {
            string tmp;
            for(int i=0 ; i < path.size()-1 ;i++ )
            {
                tmp += to_string(path[i]);
                tmp += "->";
            }
            tmp += to_string(path[path.size()-1]);
            result.push_back(tmp);
            return;
        }
        if(cur->left != nullptr  )
        {   
           get_road(cur->left,result,path);
           path.pop_back();
        } 
        if(cur->right != nullptr)
        {
           get_road(cur->right,result,path);
           path.pop_back();
        }
        return;
    }
    vector<string> binaryTreePaths(TreeNode* root) {
        vector<string> result;
        vector<int> path;
        if(root == nullptr) return result;
        get_road(root ,result , path);
        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:
    void get_road(TreeNode* cur  , vector<string> &result , string path)
    {
        if(cur == nullptr) return;
        path +=  to_string(cur->val);
        if(cur->left == nullptr && cur->right == nullptr )
        {
            result.push_back(path);
            return;
        }
        if(cur->left != nullptr  )
        {   
           get_road(cur->left,result, path + "->");
        } 
        if(cur->right != nullptr)
        {
           get_road(cur->right,result, path + "->");
           path.pop_back();
        }
        return;
    }
    vector<string> binaryTreePaths(TreeNode* root) {
        vector<string> result;
        string path;
        if(root == nullptr) return result;
        get_road(root ,result , path);
        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> result;
    void back_trak(TreeNode* cur , string tmp)
    {
        if(cur == nullptr) return;
        if(cur->left == nullptr && cur->right == nullptr) 
        {
            tmp += to_string(cur->val);
            result.push_back(tmp);
            return;
        };
        tmp += to_string(cur->val);
        tmp += "->";
        back_trak(cur->left,tmp);
        back_trak(cur->right,tmp);
        return;
    }
    vector<string> binaryTreePaths(TreeNode* root) {
        if(root == nullptr) return result;
        string tmp;
        back_trak(root,tmp);
        return result;
    }
};


相关文章
|
3月前
【LeetCode 35】112.路径总和
【LeetCode 35】112.路径总和
35 0
|
3月前
【LeetCode 36】113.路径总和II
【LeetCode 36】113.路径总和II
36 0
|
3月前
【LeetCode 31】104.二叉树的最大深度
【LeetCode 31】104.二叉树的最大深度
32 2
|
3月前
【LeetCode 29】226.反转二叉树
【LeetCode 29】226.反转二叉树
29 2
|
3月前
【LeetCode 28】102.二叉树的层序遍历
【LeetCode 28】102.二叉树的层序遍历
24 2
|
3月前
【LeetCode 43】236.二叉树的最近公共祖先
【LeetCode 43】236.二叉树的最近公共祖先
27 0
|
3月前
【LeetCode 38】617.合并二叉树
【LeetCode 38】617.合并二叉树
25 0
|
3月前
【LeetCode 37】106.从中序与后序遍历构造二叉树
【LeetCode 37】106.从中序与后序遍历构造二叉树
29 0
|
3月前
【LeetCode 34】257.二叉树的所有路径
【LeetCode 34】257.二叉树的所有路径
31 0
|
3月前
【LeetCode 32】111.二叉树的最小深度
【LeetCode 32】111.二叉树的最小深度
22 0