codeparrot/xlcost-text-to-code huggingface dataset:
Smallest value in each level of Binary Tree | CPP program to print smallest element in each level of binary tree . ; A Binary Tree Node ; return height of tree ; Inorder Traversal Search minimum element in each level and store it into vector array . ; height of tree for the size of vector array ; vector for store all minimum of every level ; save every level minimum using inorder traversal ; print every level minimum ; Utility function to create a new tree node ; Driver program to test above functions ; Let us create binary tree shown in above diagram ; 7 / \ 6 5 / \ / \ 4 3 2 1
#include <bits/stdc++.h> NEW_LINE #define INT_MAX  10e6 NEW_LINE using namespace std ; struct Node { int data ; struct Node * left , * right ; } ; int heightoftree ( Node * root ) { if ( root == NULL ) return 0 ; int left = heightoftree ( root -> left ) ; int right = heightoftree ( root -> right ) ; return ( ( left > right ? left : right ) + 1 ) ; } void printPerLevelMinimum ( Node * root , vector < int > & res , int level ) { if ( root != NULL ) { printPerLevelMinimum ( root -> left , res , level + 1 ) ; if ( root -> data < res [ level ] ) res [ level ] = root -> data ; printPerLevelMinimum ( root -> right , res , level + 1 ) ; } } void perLevelMinimumUtility ( Node * root ) { int n = heightoftree ( root ) , i ; vector < int > res ( n , INT_MAX ) ; printPerLevelMinimum ( root , res , 0 ) ; cout << " Every ▁ level ▁ minimum ▁ is STRNEWLINE " ; for ( i = 0 ; i < n ; i ++ ) { cout << " level ▁ " << i << " ▁ min ▁ is ▁ = ▁ " << res [ i ] << " STRNEWLINE " ; } } Node * newNode ( int data ) { Node * temp = new Node ; temp -> data = data ; temp -> left = temp -> right = NULL ; return temp ; } int main ( ) { Node * root = newNode ( 7 ) ; root -> left = newNode ( 6 ) ; root -> right = newNode ( 5 ) ; root -> left -> left = newNode ( 4 ) ; root -> left -> right = newNode ( 3 ) ; root -> right -> left = newNode ( 2 ) ; root -> right -> right = newNode ( 1 ) ; perLevelMinimumUtility ( root ) ; return 0 ; }
Smallest value in each level of Binary Tree | CPP program to print minimum element in each level of binary tree . ; A Binary Tree Node ; return height of tree ; Iterative method to find every level minimum element of Binary Tree ; Base Case ; Create an empty queue for level order traversal ; push the root for Change the level ; for go level by level ; for check the level ; Get top of queue ; if node == NULL ( Means this is boundary between two levels ) ; here queue is empty represent no element in the actual queue ; increment level ; Reset min for next level minimum value ; get Minimum in every level ; Enqueue left child ; Enqueue right child ; Utility function to create a new tree node ; Driver program to test above functions ; Let us create binary tree shown in above diagram ; 7 / \ 6 5 / \ / \ 4 3 2 1
#include <iostream> NEW_LINE #include <queue> NEW_LINE #include <vector> NEW_LINE #define INT_MAX  10e6 NEW_LINE using namespace std ; struct Node { int data ; struct Node * left , * right ; } ; int heightoftree ( Node * root ) { if ( root == NULL ) return 0 ; int left = heightoftree ( root -> left ) ; int right = heightoftree ( root -> right ) ; return ( ( left > right ? left : right ) + 1 ) ; } void printPerLevelMinimum ( Node * root ) { if ( root == NULL ) return ; queue < Node * > q ; q . push ( root ) ; q . push ( NULL ) ; int min = INT_MAX ; int level = 0 ; while ( q . empty ( ) == false ) { Node * node = q . front ( ) ; q . pop ( ) ; if ( node == NULL ) { cout << " level ▁ " << level << " ▁ min ▁ is ▁ = ▁ " << min << " STRNEWLINE " ; if ( q . empty ( ) ) break ; q . push ( NULL ) ; level ++ ; min = INT_MAX ; continue ; } if ( min > node -> data ) min = node -> data ; if ( node -> left != NULL ) { q . push ( node -> left ) ; } if ( node -> right != NULL ) { q . push ( node -> right ) ; } } } Node * newNode ( int data ) { Node * temp = new Node ; temp -> data = data ; temp -> left = temp -> right = NULL ; return temp ; } int main ( ) { Node * root = newNode ( 7 ) ; root -> left = newNode ( 6 ) ; root -> right = newNode ( 5 ) ; root -> left -> left = newNode ( 4 ) ; root -> left -> right = newNode ( 3 ) ; root -> right -> left = newNode ( 2 ) ; root -> right -> right = newNode ( 1 ) ; cout << " Every ▁ Level ▁ minimum ▁ is " << " STRNEWLINE " ; printPerLevelMinimum ( root ) ; return 0 ; }
Count occurrences of a string that can be constructed from another given string | C ++ implementation of the above approach ; Function to find the count ; Initialize hash for both strings ; hash the frequency of letters of str1 ; hash the frequency of letters of str2 ; Find the count of str2 constructed from str1 ; Return answer ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int findCount ( string str1 , string str2 ) { int len = str1 . size ( ) ; int len2 = str2 . size ( ) ; int ans = INT_MAX ; int hash1 [ 26 ] = { 0 } , hash2 [ 26 ] = { 0 } ; for ( int i = 0 ; i < len ; i ++ ) hash1 [ str1 [ i ] - ' a ' ] ++ ; for ( int i = 0 ; i < len2 ; i ++ ) hash2 [ str2 [ i ] - ' a ' ] ++ ; for ( int i = 0 ; i < 26 ; i ++ ) if ( hash2 [ i ] ) ans = min ( ans , hash1 [ i ] / hash2 [ i ] ) ; return ans ; } int main ( ) { string str1 = " geeksclassesatnoida " ; string str2 = " sea " ; cout << findCount ( str1 , str2 ) ; return 0 ; }
Check if a string is the typed name of the given name | CPP program to implement run length encoding ; Check if the character is vowel or not ; Returns true if ' typed ' is a typed name given str ; Traverse through all characters of str . ; If current characters do not match ; If not vowel , simply move ahead in both ; Count occurrences of current vowel in str ; Count occurrences of current vowel in typed . ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool isVowel ( char c ) { string vowel = " aeiou " ; for ( int i = 0 ; i < vowel . length ( ) ; ++ i ) if ( vowel [ i ] == c ) return true ; return false ; } bool printRLE ( string str , string typed ) { int n = str . length ( ) , m = typed . length ( ) ; int j = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( str [ i ] != typed [ j ] ) return false ; if ( isVowel ( str [ i ] ) == false ) { j ++ ; continue ; } int count1 = 1 ; while ( i < n - 1 && str [ i ] == str [ i + 1 ] ) { count1 ++ ; i ++ ; } int count2 = 1 ; while ( j < m - 1 && typed [ j ] == str [ i ] ) { count2 ++ ; j ++ ; } if ( count1 > count2 ) return false ; } return true ; } int main ( ) { string name = " alex " , typed = " aaalaeex " ; if ( printRLE ( name , typed ) ) cout << " Yes " ; else cout << " No " ; return 0 ; }
Get Level of a node in a Binary Tree | C ++ program to Get Level of a node in a Binary Tree ; A tree node structure ; Helper function for getLevel ( ) . It returns level of the data if data is present in tree , otherwise returns 0. ; Returns level of given data value ; Utility function to create a new Binary Tree node ; Driver Code ; Constructing tree given in the above figure
#include <bits/stdc++.h> NEW_LINE using namespace std ; struct node { int data ; struct node * left ; struct node * right ; } ; int getLevelUtil ( struct node * node , int data , int level ) { if ( node == NULL ) return 0 ; if ( node -> data == data ) return level ; int downlevel = getLevelUtil ( node -> left , data , level + 1 ) ; if ( downlevel != 0 ) return downlevel ; downlevel = getLevelUtil ( node -> right , data , level + 1 ) ; return downlevel ; } int getLevel ( struct node * node , int data ) { return getLevelUtil ( node , data , 1 ) ; } struct node * newNode ( int data ) { struct node * temp = new struct node ; temp -> data = data ; temp -> left = NULL ; temp -> right = NULL ; return temp ; } int main ( ) { struct node * root = new struct node ; int x ; root = newNode ( 3 ) ; root -> left = newNode ( 2 ) ; root -> right = newNode ( 5 ) ; root -> left -> left = newNode ( 1 ) ; root -> left -> right = newNode ( 4 ) ; for ( x = 1 ; x <= 5 ; x ++ ) { int level = getLevel ( root , x ) ; if ( level ) cout << " Level ▁ of ▁ " << x << " ▁ is ▁ " << getLevel ( root , x ) << endl ; else cout << x << " is ▁ not ▁ present ▁ in ▁ tree " << endl ; } getchar ( ) ; return 0 ; }
Program to replace a word with asterisks in a sentence | C ++ program to censor a word with asterisks in a sentence ; Function takes two parameter ; Break down sentence by ' ▁ ' spaces and store each individual word in a different list ; A new string to store the result ; Creating the censor which is an asterisks " * " text of the length of censor word ; Iterating through our list of extracted words ; changing the censored word to created asterisks censor ; join the words ; Driver code
#include <bits/stdc++.h> NEW_LINE #include <boost/algorithm/string.hpp> NEW_LINE using namespace std ; string censor ( string text , string word ) { vector < string > word_list ; boost :: split ( word_list , text , boost :: is_any_of ( " \\ ▁ + " ) ) ; string result = " " ; string stars = " " ; for ( int i = 0 ; i < word . size ( ) ; i ++ ) stars += ' * ' ; int index = 0 ; for ( string i : word_list ) { if ( i . compare ( word ) == 0 ) { word_list [ index ] = stars ; } index ++ ; } for ( string i : word_list ) { result += i + ' ▁ ' ; } return result ; } int main ( ) { string extract = " GeeksforGeeks ▁ is ▁ a ▁ computer ▁ science ▁ " " portal ▁ for ▁ geeks . ▁ I ▁ am ▁ pursuing ▁ my ▁ " " major ▁ in ▁ computer ▁ science . ▁ " ; string cen = " computer " ; cout << ( censor ( extract , cen ) ) ; }
Finite Automata algorithm for Pattern Searching | CPP program for Finite Automata Pattern searching Algorithm ; If the character c is same as next character in pattern , then simply increment state ; ns stores the result which is next state ; Start from the largest possible value and stop when you find a prefix which is also suffix ; This function builds the TF table which represents4 Finite Automata for a given pattern ; Prints all occurrences of pat in txt ; Process txt over FA . ; Driver program to test above function
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define NO_OF_CHARS  256 NEW_LINE int getNextState ( string pat , int M , int state , int x ) { if ( state < M && x == pat [ state ] ) return state + 1 ; int ns , i ; for ( ns = state ; ns > 0 ; ns -- ) { if ( pat [ ns - 1 ] == x ) { for ( i = 0 ; i < ns - 1 ; i ++ ) if ( pat [ i ] != pat [ state - ns + 1 + i ] ) break ; if ( i == ns - 1 ) return ns ; } } return 0 ; } void computeTF ( string pat , int M , int TF [ ] [ NO_OF_CHARS ] ) { int state , x ; for ( state = 0 ; state <= M ; ++ state ) for ( x = 0 ; x < NO_OF_CHARS ; ++ x ) TF [ state ] [ x ] = getNextState ( pat , M , state , x ) ; } void search ( string pat , string txt ) { int M = pat . size ( ) ; int N = txt . size ( ) ; int TF [ M + 1 ] [ NO_OF_CHARS ] ; computeTF ( pat , M , TF ) ; int i , state = 0 ; for ( i = 0 ; i < N ; i ++ ) { state = TF [ state ] [ txt [ i ] ] ; if ( state == M ) cout << " ▁ Pattern ▁ found ▁ at ▁ index ▁ " << i - M + 1 << endl ; } } int main ( ) { string txt = " AABAACAADAABAAABAA " ; string pat = " AABA " ; search ( pat , txt ) ; return 0 ; }
Longest Substring containing '1' | C ++ program for the above approach ; Function to find length of longest substring containing '1' ; Count the number of contiguous 1 's ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int maxlength ( string s ) { int n = s . length ( ) , i , j ; int ans = 0 ; for ( i = 0 ; i <= n - 1 ; i ++ ) { if ( s [ i ] == '1' ) { int count = 1 ; for ( j = i + 1 ; j <= n - 1 && s [ j ] == '1' ; j ++ ) count ++ ; ans = max ( ans , count ) ; } } return ans ; } int main ( ) { string s = "11101110" ; cout << maxlength ( s ) << endl ; return 0 ; }
Generate all possible strings formed by replacing letters with given respective symbols | C ++ program for the above approach ; Function to generate all possible string by replacing the characters with mapped symbols ; Base Case ; Function call with the P - th character not replaced ; Replace the P - th character ; Function call with the P - th character replaced ; Driver Code ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; void generateLetters ( string S , int P , unordered_map < char , char > M ) { if ( P == S . size ( ) ) { cout << S << " STRNEWLINE " ; return ; } generateLetters ( S , P + 1 , M ) ; S [ P ] = M [ S [ P ] ] ; generateLetters ( S , P + 1 , M ) ; return ; } int main ( ) { string S = " aBc " ; unordered_map < char , char > M ; M [ ' a ' ] = ' $ ' ; M [ ' B ' ] = ' # ' ; M [ ' c ' ] = ' ^ ' ; M [ ' d ' ] = ' & ' ; M [ '1' ] = ' * ' ; M [ '2' ] = ' ! ' ; M [ ' E ' ] = ' @ ' ; generateLetters ( S , 0 , M ) ; return 0 ; }
Max count of N using digits of M such that 2 and 5 , and , 6 and 9 can be treated as same respectively | C ++ program for the above approach ; Function to find the count of numbers that can be formed using the given digits in the string ; Store the frequency of digits from the given string M ; Store length of the string M ; Loop to traverse the string ; Replace 5 with 2 ; Replace 9 with 6 ; Get the int form of the current character in the string ; Insert in the map ; Store all the digits of the required number N ; Loop to get all the digits from the number N ; Get the last digit as the remainder ; Replace 5 with 2 ; Replace 9 with 6 ; Insert the remainders in the rems map ; Store the resultant count ; Iterate through the rems map ; Get the key which is a digit from the number N to be formed ; If not present in the string M , number N that cannot be formed ; Divide the frequency of the digit from the string M with the frequency of the current remainder ; Choose the minimum ; Return the maximum count ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int solve ( int n , string str ) { map < int , int > mymap ; int len = str . size ( ) ; for ( int i = 0 ; i < len ; i ++ ) { char c = str [ i ] ; if ( c == '5' ) c = '2' ; else if ( c == '9' ) c = '6' ; int c_int = c - ' a ' ; if ( mymap . find ( c_int ) != mymap . end ( ) ) mymap [ c_int ] += 1 ; else mymap [ c_int ] = 1 ; } map < int , int > rems ; while ( n > 0 ) { int rem = n % 10 ; if ( rem == 5 ) rem = 2 ; if ( rem == 9 ) rem = 6 ; if ( rems . find ( rem ) != rems . end ( ) ) rems [ rem ] += 1 ; else rems [ rem ] = 1 ; n = n / 10 ; } int cnt = 1e8 ; for ( auto ele : rems ) { int key = ele . first ; if ( mymap . find ( key ) == mymap . end ( ) ) return 0 ; int temp = mymap [ key ] / ele . second ; cnt = min ( cnt , temp ) ; } return cnt ; } int main ( ) { int N = 56 ; string M = "245769" ; cout << solve ( N , M ) << endl ; return 0 ; }
Minimize swaps of pairs of characters required such that no two adjacent characters in the string are same | C ++ program for the above approach ; Function to check if S contains any pair of adjacent characters that are same ; Traverse the string S ; If current pair of adjacent characters are the same ; Return true ; Utility function to find the minimum number of swaps of pair of characters required to make all pairs of adjacent characters different ; Check if the required string is formed already ; Traverse the string S ; Swap the characters at i and j position ; Swap for Backtracking Step ; Function to find the minimum number of swaps of pair of characters required to make all pairs of adjacent characters different ; Stores the resultant minimum number of swaps required ; Function call to find the minimum swaps required ; Print the result ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool check ( string & S ) { for ( int i = 1 ; i < S . length ( ) ; i ++ ) { if ( S [ i - 1 ] == S [ i ] ) { return false ; } } return true ; } void minimumSwaps ( string & S , int & ansSwaps , int swaps = 0 , int idx = 0 ) { if ( check ( S ) ) { ansSwaps = min ( ansSwaps , swaps ) ; } for ( int i = idx ; i < S . length ( ) ; i ++ ) { for ( int j = i + 1 ; j < S . length ( ) ; j ++ ) { swap ( S [ i ] , S [ j ] ) ; minimumSwaps ( S , ansSwaps , swaps + 1 , i + 1 ) ; swap ( S [ i ] , S [ j ] ) ; } } } void findMinimumSwaps ( string & S ) { int ansSwaps = INT_MAX ; minimumSwaps ( S , ansSwaps ) ; if ( ansSwaps == INT_MAX ) cout << " - 1" ; else cout << ansSwaps ; } int main ( ) { string S = " ABAACD " ; findMinimumSwaps ( S ) ; return 0 ; }
Maximum count of 0 s between two 1 s in given range for Q queries |  ; Function to count the number of 0 s lying between the two 1 s for each query ; Stores count of 0 ' s ▁ that ▁ are ▁ ▁ right ▁ to ▁ the ▁ most ▁ recent ▁ 1' s ; Stores count of 0 ' s ▁ that ▁ are ▁ ▁ left ▁ to ▁ the ▁ most ▁ recent ▁ 1' s ; Stores the count of zeros in a prefix / suffix of array ; Stores the count of total 0 s ; Traverse the string S ; If current character is '1' ; Otherwise ; Update the rightBound [ i ] ; Update count and total to 0 ; Traverse the string S in reverse manner ; If current character is '1' ; Otherwise ; Update the leftBound [ i ] ; Traverse given query array ; Update the value of count ; Print the count as the result to the current query [ L , R ] ; Driver Code
#include <iostream> NEW_LINE using namespace std ; void countOsBetween1s ( string S , int N , int Q [ ] [ 2 ] ) { int leftBound [ N ] ; int rightBound [ N ] ; int count = 0 ; int total = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( S [ i ] == '1' ) { count = total ; } else if ( S [ i ] == '0' ) { total ++ ; } rightBound [ i ] = count ; } count = 0 ; total = 0 ; for ( int i = N - 1 ; i >= 0 ; i -- ) { if ( S [ i ] == '1' ) { count = total ; } else if ( S [ i ] == '0' ) { total ++ ; } leftBound [ i ] = count ; } for ( int q = 0 ; q < 2 ; q ++ ) { int L = Q [ q ] [ 0 ] ; int R = Q [ q ] [ 1 ] ; count = leftBound [ L ] + rightBound [ R ] - total ; cout << count << " ▁ " ; } } int main ( ) { string S = "1001010" ; int Q [ ] [ 2 ] = { { 0 , 4 } , { 0 , 5 } } ; int N = S . length ( ) ; countOsBetween1s ( S , N , Q ) ; return 0 ; }
Get level of a node in binary tree | iterative approach | CPP program to print level of given node in binary tree iterative approach ; node of binary tree ; utility function to create a new node ; utility function to return level of given node ; extra NULL is pushed to keep track of all the nodes to be pushed before level is incremented by 1 ; Driver Code ; create a binary tree ; return level of node
#include <bits/stdc++.h> NEW_LINE using namespace std ; struct node { int data ; node * left ; node * right ; } ; node * getnode ( int data ) { node * newnode = new node ( ) ; newnode -> data = data ; newnode -> left = NULL ; newnode -> right = NULL ; } int getlevel ( node * root , int data ) { queue < node * > q ; int level = 1 ; q . push ( root ) ; q . push ( NULL ) ; while ( ! q . empty ( ) ) { node * temp = q . front ( ) ; q . pop ( ) ; if ( temp == NULL ) { if ( q . front ( ) != NULL ) { q . push ( NULL ) ; } level += 1 ; } else { if ( temp -> data == data ) { return level ; } if ( temp -> left ) { q . push ( temp -> left ) ; } if ( temp -> right ) { q . push ( temp -> right ) ; } } } return 0 ; } int main ( ) { node * root = getnode ( 20 ) ; root -> left = getnode ( 10 ) ; root -> right = getnode ( 30 ) ; root -> left -> left = getnode ( 5 ) ; root -> left -> right = getnode ( 15 ) ; root -> left -> right -> left = getnode ( 12 ) ; root -> right -> left = getnode ( 25 ) ; root -> right -> right = getnode ( 40 ) ; int level = getlevel ( root , 30 ) ; ( level != 0 ) ? ( cout << " level ▁ of ▁ node ▁ 30 ▁ is ▁ " << level << endl ) : ( cout << " node ▁ 30 ▁ not ▁ found " << endl ) ; level = getlevel ( root , 12 ) ; ( level != 0 ) ? ( cout << " level ▁ of ▁ node ▁ 12 ▁ is ▁ " << level << endl ) : ( cout << " node ▁ 12 ▁ not ▁ found " << endl ) ; level = getlevel ( root , 25 ) ; ( level != 0 ) ? ( cout << " level ▁ of ▁ node ▁ 25 ▁ is ▁ " << level << endl ) : ( cout << " node ▁ 25 ▁ not ▁ found " << endl ) ; level = getlevel ( root , 27 ) ; ( level != 0 ) ? ( cout << " level ▁ of ▁ node ▁ 27 ▁ is ▁ " << level << endl ) : ( cout << " node ▁ 27 ▁ not ▁ found " << endl ) ; return 0 ; }
Lexicographically smallest Palindromic Path in a Binary Tree | C ++ program for the above approach ; Struct binary tree node ; Function to create a new node ; Function to check if the string is palindrome or not ; Function to find the lexicographically smallest palindromic path in the Binary Tree ; Base case ; Append current node 's  data to the string ; Check if a node is leaf or not ; Check for the 1 st Palindromic Path ; Store lexicographically the smallest palindromic path ; Recursively traverse left subtree ; Recursively traverse right subtree ; Function to get smallest lexographical palindromic path ; Variable which stores the final result ; Function call to compute lexicographically smallest palindromic Path ; Driver Code ; Construct binary tree
#include <bits/stdc++.h> NEW_LINE using namespace std ; struct Node { char data ; Node * left , * right ; } ; Node * newNode ( char data ) { Node * temp = new Node ( ) ; temp -> data = data ; temp -> left = temp -> right = NULL ; return temp ; } bool checkPalindrome ( string s ) { int low = 0 , high = ( int ) s . size ( ) - 1 ; while ( low < high ) { if ( s [ low ] != s [ high ] ) return false ; low ++ ; high -- ; } return true ; } void lexicographicallySmall ( Node * root , string s , string & finalAns ) { if ( root == NULL ) return ; s += root -> data ; if ( ! root -> left and ! root -> right ) { if ( checkPalindrome ( s ) ) { if ( finalAns == " $ " ) finalAns = s ; else finalAns = min ( finalAns , s ) ; } return ; } lexicographicallySmall ( root -> left , s , finalAns ) ; lexicographicallySmall ( root -> right , s , finalAns ) ; } void getPalindromePath ( Node * root ) { string finalAns = " $ " ; lexicographicallySmall ( root , " " , finalAns ) ; if ( finalAns == " $ " ) cout << " No ▁ Palindromic ▁ Path ▁ exists " ; else cout << finalAns ; } int main ( ) { Node * root = newNode ( ' a ' ) ; root -> left = newNode ( ' c ' ) ; root -> left -> left = newNode ( ' a ' ) ; root -> left -> right = newNode ( ' g ' ) ; root -> right = newNode ( ' b ' ) ; root -> right -> left = newNode ( ' b ' ) ; root -> right -> right = newNode ( ' x ' ) ; root -> right -> left -> right = newNode ( ' a ' ) ; getPalindromePath ( root ) ; return 0 ; }
Find mirror of a given node in Binary tree | C ++ program to find the mirror Node in Binary tree ; A binary tree Node has data , pointer to left child and a pointer to right child ; create new Node and initialize it ; recursive function to find mirror of Node ; if any of the Node is none then Node itself and decendent have no mirror , so return none , no need to further explore ! ; if left Node is target Node , then return right 's key (that is mirror) and vice   versa  ; first recur external Nodes ; if no mirror found , recur internal Nodes ; interface for mirror search ; Driver Code ; target Node whose mirror have to be searched
#include <bits/stdc++.h> NEW_LINE using namespace std ; struct Node { int key ; struct Node * left , * right ; } ; struct Node * newNode ( int key ) { struct Node * n = ( struct Node * ) malloc ( sizeof ( struct Node * ) ) ; if ( n != NULL ) { n -> key = key ; n -> left = NULL ; n -> right = NULL ; return n ; } else { cout << " Memory ▁ allocation ▁ failed ! " << endl ; exit ( 1 ) ; } } int findMirrorRec ( int target , struct Node * left , struct Node * right ) { if ( left == NULL right == NULL ) return 0 ; if ( left -> key == target ) return right -> key ; if ( right -> key == target ) return left -> key ; int mirror_val = findMirrorRec ( target , left -> left , right -> right ) ; if ( mirror_val ) return mirror_val ; findMirrorRec ( target , left -> right , right -> left ) ; } int findMirror ( struct Node * root , int target ) { if ( root == NULL ) return 0 ; if ( root -> key == target ) return target ; return findMirrorRec ( target , root -> left , root -> right ) ; } int main ( ) { struct Node * root = newNode ( 1 ) ; root -> left = newNode ( 2 ) ; root -> left -> left = newNode ( 4 ) ; root -> left -> left -> right = newNode ( 7 ) ; root -> right = newNode ( 3 ) ; root -> right -> left = newNode ( 5 ) ; root -> right -> right = newNode ( 6 ) ; root -> right -> left -> left = newNode ( 8 ) ; root -> right -> left -> right = newNode ( 9 ) ; int target = root -> left -> left -> key ; int mirror = findMirror ( root , target ) ; if ( mirror ) cout << " Mirror ▁ of ▁ Node ▁ " << target << " ▁ is ▁ Node ▁ " << mirror << endl ; else cout << " Mirror ▁ of ▁ Node ▁ " << target << " ▁ is ▁ NULL ! ▁ " << endl ; }