Symmetric Tree

Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center).

struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };

bool isMirrorSame(TreeNode *treea, TreeNode *treeb)
{
if(!treea && !treeb) return true;
if((treea&&!treeb) || (treeb&&!treea)) return false;
if(treea->val != treeb->val) return false;
return isMirrorSame(treea->left,treeb->right) && isMirrorSame(treea->right,treeb->left);
}
bool isSymmetric(TreeNode *root) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
if(!root) return true;
return isMirrorSame(root->left,root->right);
}

Runtime: 36 ms

Resolving technical problems:

Solve your technical problems instantly

We provide Remote Technical Support from Monday to Sunday, 7:00PM to 1:00 AM

Mail your problem details at writeulearn@gmail.com along with your mobile numberand we will give you a call for further details. We usually attend your problems within 60 minutes and solve it in maximum 2 days.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.