Created
June 8, 2017 16:56
-
-
Save betterkenly/5eaeac94bee2b48277ac4249f38d21ef to your computer and use it in GitHub Desktop.
probably correct approach but wrong execution, need to refine JS skill and logic. and focus on tree. poor
This file contains hidden or 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
const findLargestLevel = function(node) { | |
var queue = []; | |
node.level = 0; | |
queue.push(node); | |
var current; | |
var largest; | |
while (queue.length) { | |
current = queue.splice(0,1)[0]; | |
if (current.left) { | |
current.left.level = current.level++; | |
temp.push(current.left); | |
} | |
if (current.right) { | |
current.right.level = current.level++; | |
temp.push(current.right); | |
} | |
} | |
var counts = queue.reduce(function(counts, each ){ | |
if (!counts[each.leve]){ | |
counts[each.level] = each.value; | |
} else { | |
counts[each.level] = counts[each.level] + each.value; | |
} | |
return counts; | |
},{}); | |
var answer = { | |
level : null, | |
value: 0 | |
} | |
for (var key in counts) { | |
if (counts[key] > value) { | |
answer['level'] = key; | |
answer.value = counts[key] | |
} | |
} | |
return asnwer['level']; | |
}; | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment