Simple C++ Recursive Solution


  • 1
    M
    class Solution {
    public:
        vector<string> binaryTreePaths(TreeNode* root) {
            if (!root) return {};
            if (!root->left && !root->right) return {to_string(root->val)};
            vector<string> l = binaryTreePaths(root->left), r = binaryTreePaths(root->right);
            l.insert(l.end(), r.begin(), r.end());
            for (string & s: l) s = to_string(root->val) + "->" + s;
            return l;
        }
    };
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.