wind force weak

"Code me an Input"

Input

Output

Keys Takeaways

function isRoot(node: Node<Data>) {
  return !node.hasOwnProperty("parent")
}
 
function isLeaf(node: Node<Data>) {
  return !node.hasOwnProperty("children")
}
 
function findLeafs(tree: Tree<Data>): Array<Node<Data>> {
  const leafSet = new Set<Node["id"]>()
  const leafArray: Array<Node<Data>> = []
 
  tree.forEach((node) => {
    if (!leafSet.has(node) && isLeaf(node)) {
      leafSet.add(node.id)
      leafArray.push(node)
    }
  })
 
  return leafArray
}