15ms


  • 0
    C

    class Solution {
    public:
    void makestring(TreeNode root,string &s )
    {
    if(!root->left&&!root->right) return ;
    if(root->left) { s+='(';s+=to_string(root->left->val);makestring(root->left,s);s+=')';}
    else s+="()";
    if(root->right) { s+='(';s+=to_string(root->right->val);makestring(root->right,s);s+=')';}
    }
    string tree2str(TreeNode
    t) {
    string ans;
    if(!t) return ans;
    ans+=to_string(t->val);
    makestring(t,ans);
    return ans;

    }
    

    };


Log in to reply
 

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