Find the closest leaf in a Binary Tree | C # program to find closest leaf of a given key in Binary Tree ; Class containing left and right child of current node and key value ; 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 for above functions ; Let us construct the BST shown in the above figure
using System ; public class Node { public int data ; public Node left , right ; public Node ( int item ) { data = item ; left = right = null ; } } public class BinaryTree { public Node root ; public virtual int getMin ( int x , int y ) { return ( x < y ) ? x : y ; } public virtual int closestDown ( Node node ) { if ( node == null ) { return int . MaxValue ; } if ( node . left == null && node . right == null ) { return 0 ; } return 1 + getMin ( closestDown ( node . left ) , closestDown ( node . right ) ) ; } public virtual int findClosestUtil ( Node node , char k , Node [ ] ancestors , int index ) { if ( node == null ) { return int . MaxValue ; } if ( ( char ) node . data == k ) { int res = closestDown ( node ) ; for ( int i = index - 1 ; i >= 0 ; i -- ) { res = getMin ( res , index - i + closestDown ( ancestors [ i ] ) ) ; } return res ; } ancestors [ index ] = node ; return getMin ( findClosestUtil ( node . left , k , ancestors , index + 1 ) , findClosestUtil ( node . right , k , ancestors , index + 1 ) ) ; } public virtual int findClosest ( Node node , char k ) { Node [ ] ancestors = new Node [ 100 ] ; return findClosestUtil ( node , k , ancestors , 0 ) ; } public static void Main ( string [ ] args ) { BinaryTree tree = new BinaryTree ( ) ; tree . root = new Node ( ' A ' ) ; tree . root . left = new Node ( ' B ' ) ; tree . root . right = new Node ( ' C ' ) ; tree . root . right . left = new Node ( ' E ' ) ; tree . root . right . right = new Node ( ' F ' ) ; tree . root . right . left . left = new Node ( ' G ' ) ; tree . root . right . left . left . left = new Node ( ' I ' ) ; tree . root . right . left . left . right = new Node ( ' J ' ) ; tree . root . right . right . right = new Node ( ' H ' ) ; tree . root . right . right . right . left = new Node ( ' H ' ) ; char k = ' H ' ; Console . WriteLine ( " Distance ▁ of ▁ the ▁ closest ▁ key ▁ from ▁ " + k + " ▁ is ▁ " + tree . findClosest ( tree . root , k ) ) ; k = ' C ' ; Console . WriteLine ( " Distance ▁ of ▁ the ▁ closest ▁ key ▁ from ▁ " + k + " ▁ is ▁ " + tree . findClosest ( tree . root , k ) ) ; k = ' E ' ; Console . WriteLine ( " Distance ▁ of ▁ the ▁ closest ▁ key ▁ from ▁ " + k + " ▁ is ▁ " + tree . findClosest ( tree . root , k ) ) ; k = ' B ' ; Console . WriteLine ( " Distance ▁ of ▁ the ▁ closest ▁ key ▁ from ▁ " + k + " ▁ is ▁ " + tree . findClosest ( tree . root , k ) ) ; } }