Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center).
Example :
`1
/ \ 2 2 / \ / \ 3 4 4 3`
The above binary tree is symmetric.But the following is not:
`1
/ \ 2 2 \ \ 3 3`
Return 0 / 1 ( 0 for false, 1 for true ) for this problem
Symmetric Binary Tree - InterviewBit
/**
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
int solve(TreeNode* root1,TreeNode* root2)
{
if(!root1&&!root2)
return 1;
if(!root1||!root2)
return 0;
if(root1->val!=root2->val)
return 0;
return solve(root1->left,root2->right)&&solve(root1->right,root2->left);
}
int Solution::isSymmetric(TreeNode* root) {
if(!root)
return 0;
if(!root->left && !root->right)
return 1;
return solve(root->left,root->right);
}