Intuitive Javascript Solution

  • 0
    var tree2str = function(t) {
        if (!t) return '';
        const left = tree2str(t.left);
        const right = tree2str(t.right);
        // omit printing empty right node in the string
        if (right) return `${t.val}(${left})(${right})`;
        else if (left) return `${t.val}(${left})`;
        else return `${t.val}`;

Log in to reply

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