C++ easy recursive solution


  • 0
    S
    string tree2str(TreeNode* t) {
        if (!t)
            return "";
        string str = to_string(t->val);
        if (t->right)
            str += "(" + tree2str(t->left) + ")(" + tree2str(t->right) + ")";
        else if (t->left)
            str += "(" + tree2str(t->left) + ")";
        return str;
    }
    

Log in to reply
 

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