Skip to content

Instantly share code, notes, and snippets.

@felipecodes
Created March 1, 2019 03:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save felipecodes/1fc14bf858ebb994f8b5cd918a3e0f2a to your computer and use it in GitHub Desktop.
Save felipecodes/1fc14bf858ebb994f8b5cd918a3e0f2a to your computer and use it in GitHub Desktop.
tree exercise
function traverse(node, callback) {
if (node !== null) {
traverse(node.l, callback);
callback(node.x)
traverse(node.r, callback);
}
}
function solution(T) {
const visibles = [];
traverse(T, x => {
if (x >= T.x) {
visibles.push(x);
}
})
return visibles;
}
const T = {
x: 8,
l: {
x: 2,
l: {
x: 8,
l: null,
r: null
},
r: null
},
r: {
x: 5,
l: null,
r: null
}
};
console.log(solution(T));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment