Q:
Given a Two Binary Trees, write a function that returns true if one is mirror of other, else returns false
belongs to collection: Interview C++ coding problems/challenges | tree
Interview C++ coding problems/challenges | tree
- Find the level in a binary tree with given sum K
- Check whether a Binary Tree is BST (Binary Search Tree) or not
- Print vertical sum of a binary tree
- Print Boundary Sum of a Binary Tree
- Given a Binary Tree T and a sum S, write a program to check whether there is a root to leaf path in that tree with the input sum S
- Given a Binary Tree write a program to print the nodes which don’t have a sibling node. Print all the nodes separated by space which do not have sibling in the tree in sorted order if every node has a tree than print -1
- Given a Two Binary Trees, write a function that returns true if one is mirror of other, else returns false
- Given a Binary Tree where each node has positive and negative values. Convert this to a tree where each node contains the sum of the left and right sub trees in the original tree. The values of leaf nodes are changed to 0
- Given a binary Tree, check whether the tree is symmetric or not
- Write a program to print Reverse Level Order Traversal of a binary tree
- Given an array where elements are sorted in ascending order, convert it to a height balanced BST. For this problem, a height-balanced binary tree is defined as a binary tree in which the depth of the two subtrees of every node never differ by more than 1
- Given a Binary Tree, write a function getLevelDiff which returns the difference between the sum of nodes at odd level and the sum of nodes at even level
- Write a function to detect if two trees are isomorphic
- Given an expression tree evaluate the expression tree
- Given a Binary Tree and a number K. Print all nodes that are at distance K from root (root is considered at distance 0 from itself)
- Given a Binary Tree, print Right view of it. Right view of a Binary Tree is set of nodes visible when tree is visited from Right side
- Given a Binary Tree, find diameter of it. The diameter of a tree is the number of nodes on the longest path between two leaves in the tree
- Given a BST and a value x, write a function to delete the nodes having values greater than or equal to x. The function will return the modified root
- Given a binary tree, print the diagonal traversal of the binary tree
- Given a Binary Tree, Print the corner nodes at each level. The node at the leftmost and the node at the rightmost
- Given a Binary Search Tree and 2 nodes value n1 and n2, your task is to find the lowest common ancestor of the two nodes. Assume that n1 and n2 both existing node value of the tree
- Given a string that contains ternary expressions. The expressions may be nested. You need to convert the given ternary expression to a binary Tree and return the root
- Given a binary tree, print the bottom view from left to right
- Given a Binary Tree and a target key, write a function that prints all the ancestors of the key in the given binary tree
- Given a Binary Tree of size N, write a program that prints all the possible paths from root node to the all the leaf node\'s of the binary tree
- Given a binary tree, where every node value is a number between 0-9. Find the sum of all the numbers which are formed from root to leaf paths
- Given a binary tree, and two node values your task is to find the minimum distance between them
- Find the k-th smallest element in a given binary search tree (BST)
- Write a program to print Level Order Traversal in spiral form of a binary tree
- Given a binary Tree find the maximum path sum. The path may start and end at any node in the tree
- Given an array pre[] of N nodes representing preorder traversal of BST. The task is to print its postorder traversal
- Given two n-ary trees, the task is to check if they are mirrors of each other or not
- Find number of nodes in a complete Binary Tree

C++ programming
Algorithm:
1. Define tree structure like: class tree{ // tree node is defined public: int data; tree *left; tree *right; }; Or you can use struct instead of class 2. Build both of the input tree. 3. Recursive function AreMirror to check whether both trees are mirror tree or not. FUNCTION AreMirror (root of first tree, root of second tree) Root of first tree, root1 Root of second tree root2 FUNCTION AreMirror (root1, root2) a. Check base cases If root1==NULL&&root2==NULL Then it’s mirror tree, return true; If root1==NULL || root2==NULL Then it’s not a mirror tree, return false Because one root is NULL whether another is not. (Both can’t be NULL here, since already checked before) If root1->data != root2->data Then it’s not a mirror tree, return false. Because roots are different & thus can’t be mirror image of other b. Recursively check for sub-trees return(AreMirror(root1->left, root2->right) &&AreMirror(root1->right,root2->left)); END FUNCTIONExample & Explanation:
Let’s check how the program works for the first example...
N.B: Nodes are represented by their respective values.
Root1=1 and Root2=1 In the main it calls AreMirror (1, 1) -------------------------------------------------- AreMirror (1, 1): 1->left =2 and 1->right=3 in case of tree1 1->left =3 and 1->right=2 in case of tree2 No base cases are satisfied thus it returns, (AreMirror ( 2, 2) && AreMirror ( 3, 3)) Call to AreMirror ( 2, 2) and AreMirror ( 3, 3) -------------------------------------------------- AreMirror (2, 2):(call at AreMirror (1, 1)) 2->left =4 and 2->right=NULL in case of tree1 2->left =NULL and 2->right=4 in case of tree2 No base cases are satisfied thus it returns, (AreMirror (4, 4) && AreMirror (NULL, NULL)) Call to AreMirror (4, 4) and AreMirror (NULL, NULL)) -------------------------------------------------- AreMirror (3, 3): (call at AreMirror (1, 1)) 3->left =5 and 3->right=NULL in case of tree1 3->left =NULL and 3->right=5 in case of tree2 No base cases are satisfied thus it returns, (AreMirror (5, 5) && AreMirror (NULL, NULL)) Call to AreMirror (5, 5) && AreMirror (NULL, NULL) -------------------------------------------------- AreMirror (4, 4): (call at AreMirror (2, 2)) 4->left =NULL and 4->right=NULL in case of tree1 4->left =NULL and 4->right=NULL in case of tree2 No base cases are satisfied thus it returns, (AreMirror (NULL, NULL) && AreMirror (NULL, NULL)) Call to AreMirror (NULL, NULL) && AreMirror (NULL, NULL) -------------------------------------------------- AreMirror (NULL, NULL): (call at AreMirror (2, 2)) Base case matches and returns 1. -------------------------------------------------- AreMirror (5, 5): (call at AreMirror (3, 3)) 5->left =NULL and 5->right=NULL in case of tree1 5->left =NULL and 5->right=NULL in case of tree2 No base cases are satisfied thus it returns, (AreMirror (NULL, NULL) && AreMirror (NULL, NULL)) Call to AreMirror (NULL, NULL) && AreMirror (NULL, NULL) -------------------------------------------------- AreMirror (NULL, NULL): (call at AreMirror (3, 3)) Base case matches and returns 1. -------------------------------------------------- AreMirror (NULL, NULL): (call at AreMirror (4, 4)) Base case matches and returns 1. -------------------------------------------------- AreMirror (NULL, NULL): (call at AreMirror (4, 4)) Base case matches and returns 1. -------------------------------------------------- AreMirror (NULL, NULL): (call at AreMirror (5, 5)) Base case matches and returns 1. -------------------------------------------------- AreMirror (NULL, NULL): (call at AreMirror (5, 5)) Base case matches and returns 1. -------------------------------------------------- Thus at main, AreMirror (1, 1) returns: = AreMirror ( 2, 2) && AreMirror ( 3, 3) = (AreMirror (4, 4) && AreMirror (NULL, NULL)) && (AreMirror (5, 5) && AreMirror (NULL, NULL)) = ((AreMirror (NULL, NULL) && AreMirror (NULL, NULL)) && 1) &&((AreMirror (NULL, NULL) && AreMirror (NULL, NULL)) && 1) = ((1 && 1) &&1) && (1 && 1) &&1) = 1 Thus they are mirror treesC++ implementation to check whether two trees are mirros?
Output
need an explanation for this answer? contact us directly to get an explanation for this answer