Find the closest leaf in a Binary Tree | A C ++ program to find the closesr leaf of a given key in Binary Tree ; A binary tree Node has key , pocharer to left and right children ; Helper function that allocates a new node with the given data and NULL left and right pocharers . ; A utility function to find minimum of x and y ; A utility function to find distance of closest leaf of the tree rooted under given root ; Base cases ; Return minimum of left and right , plus one ; Returns distance of the cloest leaf to a given key ' k ' . The array ancestors is used to keep track of ancestors of current node and ' index ' is used to keep track of curremt index in ' ancestors [ ] ' ; Base case ; If key found ; Find the cloest leaf under the subtree rooted with given key ; Traverse all ancestors and update result if any parent node gives smaller distance ; If key node found , store current node and recur for left and right childrens ; The main function that returns distance of the closest key to ' k ' . It mainly uses recursive function findClosestUtil ( ) to find the closes distance . ; Create an array to store ancestors Assumptiom : Maximum height of tree is 100 ; Driver program to test above functions ; Let us construct the BST shown in the above figure
#include <bits/stdc++.h> NEW_LINE using namespace std ; struct Node { char key ; struct Node * left , * right ; } ; Node * newNode ( char k ) { Node * node = new Node ; node -> key = k ; node -> right = node -> left = NULL ; return node ; } int getMin ( int x , int y ) { return ( x < y ) ? x : y ; } int closestDown ( struct Node * root ) { if ( root == NULL ) return INT_MAX ; if ( root -> left == NULL && root -> right == NULL ) return 0 ; return 1 + getMin ( closestDown ( root -> left ) , closestDown ( root -> right ) ) ; } int findClosestUtil ( struct Node * root , char k , struct Node * ancestors [ ] , int index ) { if ( root == NULL ) return INT_MAX ; if ( root -> key == k ) { int res = closestDown ( root ) ; for ( int i = index - 1 ; i >= 0 ; i -- ) res = getMin ( res , index - i + closestDown ( ancestors [ i ] ) ) ; return res ; } ancestors [ index ] = root ; return getMin ( findClosestUtil ( root -> left , k , ancestors , index + 1 ) , findClosestUtil ( root -> right , k , ancestors , index + 1 ) ) ; } int findClosest ( struct Node * root , char k ) { struct Node * ancestors [ 100 ] ; return findClosestUtil ( root , k , ancestors , 0 ) ; } int main ( ) { struct Node * root = newNode ( ' A ' ) ; root -> left = newNode ( ' B ' ) ; root -> right = newNode ( ' C ' ) ; root -> right -> left = newNode ( ' E ' ) ; root -> right -> right = newNode ( ' F ' ) ; root -> right -> left -> left = newNode ( ' G ' ) ; root -> right -> left -> left -> left = newNode ( ' I ' ) ; root -> right -> left -> left -> right = newNode ( ' J ' ) ; root -> right -> right -> right = newNode ( ' H ' ) ; root -> right -> right -> right -> left = newNode ( ' K ' ) ; char k = ' H ' ; cout << " Distance ▁ of ▁ the ▁ closest ▁ key ▁ from ▁ " << k << " ▁ is ▁ " << findClosest ( root , k ) << endl ; k = ' C ' ; cout << " Distance ▁ of ▁ the ▁ closest ▁ key ▁ from ▁ " << k << " ▁ is ▁ " << findClosest ( root , k ) << endl ; k = ' E ' ; cout << " Distance ▁ of ▁ the ▁ closest ▁ key ▁ from ▁ " << k << " ▁ is ▁ " << findClosest ( root , k ) << endl ; k = ' B ' ; cout << " Distance ▁ of ▁ the ▁ closest ▁ key ▁ from ▁ " << k << " ▁ is ▁ " << findClosest ( root , k ) << endl ; return 0 ; }