Question: Distance of closest leaf from given node in Binary Tree 0 2 . Problem Statement A binary tree is represented by the following structure: class

Distance of closest leaf from given node in Binary Tree 02. Problem Statement A binary tree is represented by the following structure: class Treetode: def init(self, data): self.data self.left data 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 the function to find distance of closest leaf from node with data 'k and Note: Return -1 if input tree is empty. Return O if node given is itself a leaf node Assumption: Only one node is present in tree with data 'k' Sample input tree: 2379 k: 458 Sample Output 1 The custom input format for the above case: 422347958(The first line represents 'k', in the second line the first integer represents the height of the tree, and the rest of the integers represent the level order traversal of the given tree, in second line 1 represents an empty node) Instructions : This is a template based question, DO NOT write the "main" function. Your code is judged by an automated system, do not write any additional welcome/greeting messages. "Save and Test" only checks for basic test cases, more rigorous cases will be used to judge your code while scoring. Additional score will be given for writing optimized code both in terms of memory and execution time.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!