bool mirrorTree(TreeNode *t1, TreeNode *t2){ if(t1 == NULL && t2 == NULL) return true; if((t1 == NULL && t2 != NULL) || (t1 != NULL && t2 == NULL)) return false; return (t1->val == t2->val) && mirrorTree(t1->left , t2->right) && mirrorTree(t1->right, t2->left); }
TreeNode *mirrorTree(TreeNode *root) { if (!root) return 0; TreeNode *left = root->left; root->left = mirrorTree(root->right); root->right = mirrorTree(left); return root; }
void mirrorTree(pnode *root) { pnode temp; if (*root == NULL) return; if ((*root)->left == NULL && (*root)->right == NULL) return; mirrorTree(&((*root)->left)); mirrorTree(&((*root)->right)); temp = (*root)->left; (*root)->left = (*root)->right; (*root)->right= temp; return; }
// to mirror a tree, we mirror the left and right child recursively. bool mirrorTree(BTreeNode *tree) { if(NULL == tree) return true; // exchange the left and right subtree. BTreeNode *tmpNode = tree->left; tree->left = tree->right; tree->right = tmpNode; // mirror recursively the subtrees. mirrorTree(tree->left); mirrorTree(tree->right); return true; }
bool isSymmetric(TreeNode *root) { // Start typing your C/C++ solution below // DO NOT write int main() function if(root == NULL) return true; return mirrorTree(root->left, root->right); }