Skip to content

Instantly share code, notes, and snippets.

@jchiatt
Created September 24, 2020 14:15
Show Gist options
  • Save jchiatt/7ede8cfe45e7d60d9529bcf60229a992 to your computer and use it in GitHub Desktop.
Save jchiatt/7ede8cfe45e7d60d9529bcf60229a992 to your computer and use it in GitHub Desktop.
const root = {
"id": 0,
"children": [
{
"id": 1,
"children": [
{
"id": 3,
"children": [
{
"id": 7,
"children": []
},
{
"id": 8,
"children": []
}
]
},
{
"id": 4,
"children": [
{
"id": 9,
"children": []
},
{
"id": 10,
"children": []
}
]
}
]
},
{
"id": 2,
"children": [
{
"id": 5,
"children": [
{
"id": 11,
"children": []
},
{
"id": 12,
"children": []
}
]
},
{
"id": 6,
"children": [
{
"id": 13,
"children": []
},
{
"id": 14,
"children": []
}
]
}
]
}
]
}
// write a function getLowestCommonAncestor(root, id1, id2)
// it should return the lowest common ancestor
// getLowestCommonAncestor(root, 13, 13) === 13
// getLowestCommonAncestor(root, 14, 14) === 14
// getLowestCommonAncestor(root, 13, 14) === 6
// getLowestCommonAncestor(root, 12, 14) === 2
// getLowestCommonAncestor(root, 7, 14) === 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment