struct TreeNode {
int val;
TreeNode* left;
TreeNode* right;
TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
};
bool isMirror(TreeNode* t1, TreeNode* t2) {
if (!t1 && !t2) return true;
if (!t1 || !t2) return false;
return (t1->val == t2->val) && isMirror(t1->left, t2->right) && isMirror(t1->right, t2->left);
}
bool isSymmetric(TreeNode* root) {
if (!root) return true;
return isMirror(root->left, root->right);
}
int main() {
TreeNode* root = new TreeNode(1);
root->left = new TreeNode(2);
root->right = new TreeNode(2);
root->left->left = new TreeNode(3);
root->left->right = new TreeNode(4);
root->right->left = new TreeNode(4);
root->right->right = new TreeNode(3);
bool result = isSymmetric(root);
std::cout << (result ? "true" : "false") << std::endl;
return 0;
}The given tree is symmetric because the left subtree is a mirror reflection of the right subtree.
The function isMirror recursively checks this property.
struct TreeNode {
int val;
TreeNode* left;
TreeNode* right;
TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
};
bool isMirror(TreeNode* t1, TreeNode* t2) {
if (!t1 && !t2) return true;
if (!t1 || !t2) return false;
return (t1->val == t2->val) && isMirror(t1->left, t2->right) && isMirror(t1->right, t2->left);
}
bool isSymmetric(TreeNode* root) {
if (!root) return true;
return isMirror(root->left, root->right);
}
int main() {
TreeNode* root = new TreeNode(1);
root->left = new TreeNode(2);
root->right = new TreeNode(2);
root->left->right = new TreeNode(3);
root->right->right = new TreeNode(3);
bool result = isSymmetric(root);
std::cout << (result ? "true" : "false") << std::endl;
return 0;
}The tree is not symmetric because the left and right subtrees do not mirror each other.
struct TreeNode {
int val;
TreeNode* left;
TreeNode* right;
TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
};
bool isMirror(TreeNode* t1, TreeNode* t2) {
if (!t1 && !t2) return true;
if (!t1 || !t2) return false;
return (t1->val == t2->val) && isMirror(t1->left, t2->right) && isMirror(t1->right, t2->left);
}
bool isSymmetric(TreeNode* root) {
if (!root) return true;
return isMirror(root->left, root->right);
}
int main() {
TreeNode* root = nullptr;
bool result = isSymmetric(root);
std::cout << (result ? "true" : "false") << std::endl;
return 0;
}The function isSymmetric does not check if root is null before accessing root->left and root->right.
This causes a segmentation fault at runtime.
Symmetry in trees means the left subtree is a mirror reflection of the right subtree, not necessarily identical.
struct TreeNode {
int val;
TreeNode* left;
TreeNode* right;
TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
};
bool isMirror(TreeNode* t1, TreeNode* t2) {
if (!t1 && !t2) return true;
if (!t1 || !t2) return false;
return (t1->val == t2->val) && isMirror(t1->left, t2->right) && isMirror(t1->right, t2->left);
}
bool isSymmetric(TreeNode* root) {
if (!root) return true;
return isMirror(root->left, root->right);
}
int main() {
TreeNode* root = new TreeNode(1);
root->left = new TreeNode(2);
root->right = new TreeNode(2);
root->left->left = nullptr;
root->left->right = new TreeNode(3);
root->right->left = nullptr;
root->right->right = new TreeNode(3);
bool result = isSymmetric(root);
std::cout << (result ? "true" : "false") << std::endl;
return 0;
}The tree is not symmetric because the left subtree's left child is null but the right subtree's left child is also null, but the right subtree's right child is not null, so the mirror condition fails.
