Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save betterkenly/5eaeac94bee2b48277ac4249f38d21ef to your computer and use it in GitHub Desktop.
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
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