c++ recursive solution


  • 0
    T
    class Solution {
    public:
        
        vector<string> binaryTreePaths(TreeNode* root) {
            if (!root) return vector<string>{};
            else if (!root->left && !root->right) {
                return vector<string>{to_string(root->val)};
            }
            vector<string> lchild = binaryTreePaths(root->left),
                rchild = binaryTreePaths(root->right);
            for (string &str:lchild) str = to_string(root->val) + "->" + str;
            for (string &str:rchild) str = to_string(root->val) + "->" + str;
            lchild.insert(lchild.end(), rchild.begin(), rchild.end());
            return lchild;
        }
    };
    

Log in to reply
 

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