Company: Boeing
Difficulty: medium
Distance of closest leaf from given node in Binary Tree Problem Description A binary tree is represented by the following structure: ``` class TreeNode: def __init__(self, data): self.data = data self.left = None self.right = None ``` Implement the following function: ``` def closestLeaf(tree, k): ``` The function accepts the root node of a binary tree 'tree' and an integer 'k' as its arguments. Implement and return the same. Notes: - Return -1 if input tree is empty. - Return 0 if node given is itself a leaf node. Assumption: Only one node is present in tree with data 'k'. Examples Example 1: ``` Input: tree = 5 / \ 2 7 / \ / \ 9 1 3 6 / \ / \ / \ / \ 10 11 12 15 8 14 16 18 k = 5 Output: 2 ``` Explanation: Leaf node '8' is at distance 2 from node '5' while all other leaf nodes are at distance 3, thus output is 2. The custom input format for the tree in the above example is described as follows: ``` 5 4 7 5 6 2 -1 -1 9 11 8 14 10 -1 12 15 -1 -1 16 18 -1 ``` The first line represents th