Company: Boeing
Difficulty: medium
Distance of closest leaf from given node in Binary Tree Problem Description You are given a binary tree and an integer k . Exactly one node of the tree holds the value k . Find the smallest number of edges between that node and any leaf of the tree. A leaf is a node with no left child and no right child. The closest leaf does not have to be a descendant of k . The path may walk upwards through ancestors and then down into another subtree, so both directions must be considered. Return -1 when the tree is empty. Return 0 when the node holding k is itself a leaf. Function Description Complete the function closestLeaf : long nodes[] : the level-order serialisation of the tree, in which -1 marks a missing child. long k : the value held by the node to measure from. Returns int : the number of edges from the node holding k to the nearest leaf. The serialisation is read breadth-first: the first value is the root, and for every node taken off the queue the next two values are its left and right