Skip to content

Instantly share code, notes, and snippets.

@alexitaylor
Created April 20, 2017 16:59
Show Gist options
  • Save alexitaylor/ad25ab0f5ee6920376376b9cec9c9f31 to your computer and use it in GitHub Desktop.
Save alexitaylor/ad25ab0f5ee6920376376b9cec9c9f31 to your computer and use it in GitHub Desktop.
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