Given a binary tree, check whether it is a mirror of itself.
For example, the binary tree[1,2,2,3,4,4,3] is a symmetric tree.
Notes
bonus points if you could solve it both recursively and iteratively.
/*
struct TreeNode{
int val;
TreeNode *left;
TreeNode *right;
*/
class solution{
public:
bool isSymmetric(TreeNode *root)(
if(!root)
return true;
else
return compareChild(root->left, root->right)
private:
bool compareChild(TreeNode *rroot, TreeNode *lroot){
if(!lroot)
return rroot == NULL;
if(rroot == NULL)
return false;
if(lroot->val != rroot->val)
return false;
return compareChild(rroot->left, lroot->right) && compareChild(rroot->right, lroot->left);
}
}