 ### C++ program to check for symmetric binary tree using recursion

Description:
This Program is to check for symmetric binary tree using recursion
For Example:

Input :
The Elements are : 1 ,2 ,2 ,3 ,4 ,4 ,3
Output :
Symmetric binary tree

Program :

//C++ program to check for symmetric binary tree using recursion #include <bits/stdc++.h> using namespace std; // tree node struct Node { int data; Node *left, *right; }; // returns a new tree Node Node* newNode(int data) { Node* temp = new Node(); temp->data = data; temp->left = temp->right = NULL; return temp; } //return true if they are mirror bool Mirror(struct Node *root1, struct Node *root2) { // if both empty if (root1 == NULL && root2 == NULL) return true; // if both are non empty then comparing them if (root1 && root2 && root1->data == root2->data) return Mirror(root1->left, root2->right) && Mirror(root1->right, root2->left); //if false return false; } int main() { struct Node *root = newNode(1); root->left = newNode(2); root->right = newNode(2); root->left->left = newNode(3); root->left->right = newNode(4); root->right->left = newNode(4); root->right->right = newNode(3); if(Mirror(root, root)) cout<<"Symmetric binary tree."; else cout<<"Not a Symmetric binary tree."; getchar(); return 0; } #### More Articles of Mandeep Sheoran:

Name Views Likes
C++ program to insert an element into binary tree 5900 19
C++ program to find an element into binary tree 763 16
C++ std::is_void 557 15
C++ program to find the closest element in binary search tree 879 19
C++ program to replace every element with the least greater element on its right 565 12
C++ program to delete an element into binary tree 780 24
C++ program to find maximum element between two nodes of binary search tree 684 20
C++ std::remove_copy_if with std::vectors 537 11
C++ program to print duplicate elements from the binary search tree 2839 15
C++ program to find depth of the deepest odd level node in binary tree 566 23
C++ program to remove duplicate elements from the binary search tree 1482 20
C++ std::rotate_copy with std::vector 521 14
C++ std::copy_n with std::vector 629 22
C++ std::copy_if with std::vector 1499 18
C++ program to print all the elements of binary search tree 6823 22
C++ std::reverse_copy with std::list 591 18
C++ program to print all the elements of binary tree 1130 18
C++ program to print all full nodes in a binary tree 565 25
C++ program to find sink odd nodes in binary tree 574 15
C++ std::is_copy_assignable 590 22
C++ program to check whether a binary tree is a full binary tree or not using recursion 598 19
C++ std::is_copy_constructible 605 27
C++ program to delete an element into binary search tree 2749 18
C++ std::reverse_copy with std::vector 483 18
C++ std::rotate with std::vector 575 15
C++ program to check for symmetric binary tree using recursion 587 25
C++ program to maximum sum from a tree with adjacent levels not allowed 536 15
C++ std::copy_n with std::list 581 21
C++ program to check if two trees are identical using recursion 539 15
C++ std::copy_n 857 21
C++ std::copy_if with std::list 956 19
C++ program to print the nodes at odd levels of a tree 553 13
C++ program to find lowest common ancestor in a binary tree 675 29
C++ program to find depth of the deepest odd level leaf node 480 13
C++ std::remove_copy_if with std::list 658 20
C++ program to add all greater values to every node in a given binary search tree 628 15