what is the right format of the output of the problem? ( JavaScript )

  • 0

    what's wrong with my code below? the problem itself says that i have to output a treeNode and then i did this by return the rootNode ,however the 'right' result is actually an array. and it says i got a [Object Object] output. My language is JavaScript. can anyone help me ?

    var buildTree = function(preorder, inorder) {
        return buildTreeFunc(preorder,inorder);
        function buildTreeFunc(preorder,inorder){
          if(preorder.length<=0||inorder.length<=0) return null;
          var node = new TreeNode(preorder.shift());
          var leftCount = inorder.indexOf(node.val);
          var rightCount = inorder.length-leftCount-1;
          node.left = buildTree(preorder.slice(0,leftCount),inorder.slice(0,leftCount));
          node.right = buildTree(preorder.slice(-1*rightCount,rightCount),inorder.slice(-rightCount,rightCount));
          return node;
    function TreeNode(val) {
      this.val = val;
      this.left = this.right = null;

Log in to reply

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