Javascript Interview Question Tree Binary Tree with minimal height

Introduction

Given a sorted array, write an algorithm to create a binary tree with minimal height.

function Node(left, right, value) {
    this.left = left;
    this.right = right;
    this.value = value;
}

function addToTree(ar, start, end) {
    //your code
}




function addToTree(ar, start, end) {
    if (end < start) {
        return null;
    }

    var mid = Math.floor((start+end)/2);

    var n = new Node(null, null, ar[mid]);
    n.left = addToTree(ar, start, mid - 1);
    n.right = addToTree(ar, mid + 1, end);

    return n;
}

/**
 * @class {public} Node
 *
 * A typical binary tree node.
 *
 * @param {Node} left - the left node.
 * @param {Node} right - the right node.
 * @param {Integer} value - the value of this node.
 */
function Node(left, right, value) {
    this.left = left;
    this.right = right;
    this.value = value;
}

var ar = [1, 2, 3, 4, 5, 6, 7, 8];

console.log( addToTree(ar, 0, ar.length - 1) );

/*
Output: ($ /usr/bin/node 010.js)
{ left:
   { left: { left: null, right: null, value: 1 },
     right: { left: null, right: null, value: 3 },
     value: 2 },
  right:
   { left: { left: null, right: null, value: 5 },
     right: { left: null, right: [Object], value: 7 },
     value: 6 },
  value: 4 }
*/



PreviousNext

Related