leetcode-257-二叉树的所有路径

/**

 * 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 {

扫描二维码关注公众号,回复: 9155752 查看本文章

public:

    vector<string> binaryTreePaths(TreeNode* root) {

        vector<string> res = {};

        if (root){

            if (root->left == NULL && root->right == NULL){

                res.push_back(to_string(root->val));

                return res;

            }

            else{

                vector<string> temp = binaryTreePaths(root->left);

                for (auto path:temp) res.push_back(to_string(root->val) + "->" + path);

                temp = binaryTreePaths(root->right);

                for (auto path:temp) res.push_back(to_string(root->val) + "->" + path);

            }

        }

        return res;

    }

};

发布了82 篇原创文章 · 获赞 0 · 访问量 1364

猜你喜欢

转载自blog.csdn.net/ChenD17/article/details/104280836
今日推荐