Created
April 20, 2017 16:59
-
-
Save alexitaylor/ad25ab0f5ee6920376376b9cec9c9f31 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function Node(val){ | |
this.value = val; | |
this.left = null; | |
this.right = null; | |
} | |
function BinaryTree(){ | |
this.root = null; | |
} | |
BinaryTree.prototype.push = function(val){ | |
var root = this.root; | |
if(!root){ | |
this.root = new Node(val); | |
return; | |
} | |
var currentNode = root; | |
var newNode = new Node(val); | |
while(currentNode){ | |
if(val < currentNode.value){ | |
if(!currentNode.left){ | |
currentNode.left = newNode; | |
break; | |
} | |
else{ | |
currentNode = currentNode.left; | |
} | |
} | |
else{ | |
if(!currentNode.right){ | |
currentNode.right = newNode; | |
break; | |
} | |
else{ | |
currentNode = currentNode.right; | |
} | |
} | |
} | |
} | |
const findLargestLevel = function(node) { | |
// your code here | |
var largestLevel = []; | |
if(node.root.right === null || node.root.left === null){ | |
return 0; | |
} | |
largestLevel.push(Math.max(findLargestLevel(node.root.left), findLargestLevel(node.root.right))); | |
return largestLevel; | |
}; | |
var root = new BinaryTree(); | |
root.push(3); | |
root.push(2); | |
root.push(4); | |
root.push(1); | |
root.push(5); | |
root.push(3); | |
root.push(2); | |
root.push(4); | |
root.push(1); | |
root.push(5); | |
//console.log(root); | |
console.log(findLargestLevel(root)); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment