【LeetCode】C++ :简单题 - 树 257. 二叉树的所有路径

257. 二叉树的所有路径

难度简单423

给定一个二叉树,返回所有从根节点到叶子节点的路径。

说明: 叶子节点是指没有子节点的节点。

示例:

输入:

   1
 /   \
2     3
 \
  5

输出: ["1->2->5", "1->3"]

解释: 所有根节点到叶子节点的路径为: 1->2->5, 1->3

这题需要再想想。

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    vector<string> binaryTreePaths(TreeNode* root) {
        vector<string> paths;
        constructPaths(root, "", paths);
        return paths;
    }
    void constructPaths(TreeNode* root, string path, vector<string> & paths){
        if(root != NULL){
            path += to_string(root->val);
            if(root->left == NULL && root->right == NULL){
                paths.push_back(path);
            }else{
                path += "->";
                constructPaths(root->left, path, paths);
                constructPaths(root->right, path, paths);
            }
        }
    }
};

换了一个题解看了看思路清晰了很多,但是不明白为什么下面的代码不能通过。

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    vector<string> binaryTreePaths(TreeNode* root) {
        vector<string> res;
        vector<int> path;
        if(root == NULL){
            return res;
        }
        constructPaths(root, path, res);
        return res;
    }

    void constructPaths(TreeNode* root, vector<int>& path, vector<string> & res){
        path.push_back(root->val);
        if(root->left == NULL && root->right){ //为叶子节点
            string spath;
            for (int i = 0; i < path.size() - 1; i++){
                spath += to_string(path[i]);
                spath += "->";
            }
            spath += to_string(path[path.size()-1]);
            res.push_back(spath);
            return;
        }
        if(root->left){
            constructPaths(root->left, path, res);
            path.pop_back();
        }
        if(root->right){
            constructPaths(root->right, path, res);
            path.pop_back();    //回溯
        }
    }
};

除了吃,我还能干点啥!!o(╥﹏╥)oo(╥﹏╥)o

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
    
    void constructPaths(TreeNode* root, vector<int>& path, vector<string> & res){
        path.push_back(root->val);

        if(root->left == NULL && root->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]);
            res.push_back(spath);
            return;
        }
        if(root->left){
            constructPaths(root->left, path, res);
            path.pop_back();
        }
        if(root->right){
            constructPaths(root->right, path, res);
            path.pop_back();    //回溯
        }
    }

public:
    vector<string> binaryTreePaths(TreeNode* root) {
        vector<string> res;
        vector<int> path;
        if(root == NULL){
            return res;
        }
        constructPaths(root, path, res);
        return res;
    }


};

再看一遍,大致明白了,递归和回溯不分家,这个道理好难实现啊。

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
    
    void constructPaths(TreeNode* root, string path, vector<string> & res){
        path += to_string(root->val);   //中

        if(root->left == NULL && root->right == NULL){ //为叶子节点
            res.push_back(path);
            return;
        }
        if(root->left){
            constructPaths(root->left, path + "->", res);
        }
        if(root->right){
            constructPaths(root->right, path + "->", res);
        }
    }

public:
    vector<string> binaryTreePaths(TreeNode* root) {
        vector<string> res;
        string path;
        if(root == NULL){
            return res;
        }
        constructPaths(root, path, res);
        return res;
    }


};

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值