JavaScript Solution


  • 0
    N

    A JavaScript Implementation of https://discuss.leetcode.com/topic/91308/java-solution-tree-traversal

    var tree2str = function(t) {
        if(t === null)
            return '';
        
        var result = t.val + '';
        
        var left = tree2str(t.left);
        var right = tree2str(t.right);
    
        if(left === '' && right === '')  {
           return result;
        } else if(left === '') {
           result += '()' + '(' + right + ')';
        } else if (right === '') {
           result += '(' + left + ')';        
        } else {
          result += '(' + left + ')' + '(' + right + ')';  
        }
        return result;
    };
    

Log in to reply
 

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