My simple c++ recursive solution, easy to understand.

  • 0
    class Solution {
        vector<string> binaryTreePaths(TreeNode* root) 
            if (!root) return {};
            auto la(binaryTreePaths(root->left));
            auto lb(binaryTreePaths(root->right));
            la.insert(la.end(), lb.begin(), lb.end());
            if (la.empty())
                return {to_string(root->val)};
            for (auto &s : la)
                s = to_string(root->val) + "->" + s;
            return la;

Log in to reply

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