C++ Solution, Tree Traversal


  • 0
    S
    class Solution {
    public:
        string tree2str(TreeNode* t) {
            if(t==nullptr)
                return "";
            string retS;
            if(t->left==nullptr&&t->right==nullptr)
                return to_string(t->val);
            retS+=to_string(t->val);
            retS+="("+tree2str(t->left)+")";
            if(t->right)
            retS+="("+tree2str(t->right)+")";
            return retS;
        }
    };
    

Log in to reply
 

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