LeetCode Problem 101 Description
Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center).
For example, this binary tree [1,2,2,3,4,4,3]
is symmetric:
1
/ \
2 2
/ \ / \
3 4 4 3
But the following [1,2,2,null,3,null,3]
is not:
1
/ \
2 2
\ \
3 3
Solution
We can solve this problem with iterative method and recursive method either and I will show my code about them. However, the most important rule of both the methods is that for the left child and the right child of the ancestor there must be left->left->val = right->right->val and left->right->val = right->left->val so that the tree is symmetric. What we need to do is to compare the preceding two equations repeatedly.
Recursive method
As we can see, I set an additional method called treeComapre to find whether the left child is the same as the right child. And it will recursively compare the pair of nodes.
#include <iostream>
#include <vector>
#include <queue>
using namespace std;
struct TreeNode {
int val;
TreeNode * left;
TreeNode * right;
TreeNode (int x) : val(x), left(NULL), right(NULL) {}
};
class Solution {
public:
bool isSymmetric (TreeNode * root);
};
//Using the recursive method to achieve it
bool treeCompare (TreeNode * left, TreeNode * right) {
if (left == NULL && right == NULL) {
return true;
}
else if (left == NULL || right == NULL) {
return false;
}
else {
if (left->val != right->val) {
return false;
}
}
return true && treeCompare(left->left, right->right) && treeCompare(left->right, right->left);
}
bool Solution::isSymmetric (TreeNode * root) {
if (root == NULL) {
return true;
}
else {
return treeCompare (root->left, root->right);
}
}
Iterative method
Just like the recursive method, what we need to do is to find whether the corresponding nodes are symmetric with the two important rules. However, we should compare the direct child of the root first since the following loop of comparision is based on two tree nodes. And we use the BFS algorithm to achieve this. Using a FIFO queue to store the piars of nodes.
#include <iostream>
#include <vector>
#include <queue>
using namespace std;
struct TreeNode {
int val;
TreeNode * left;
TreeNode * right;
TreeNode (int x) : val(x), left(NULL), right(NULL) {}
};
class Solution {
public:
bool isSymmetric (TreeNode * root);
};
//Using the iterative method to compare them, and it is BFS
bool Solution::isSymmetric (TreeNode * root) {
queue<TreeNode *> temp;
//Compare the direct child of the root
if (root == NULL) {
return true;
}
else if (root->left == NULL && root->right == NULL) {
return true;
}
else if (root->left == NULL || root->right == NULL) {
return false;
}
else {
if (root->left->val != root->right->val) {
return false;
}
}
TreeNode * l = root->left, * r = root->right;
temp.push(l);
temp.push(r);
//Compare the rest nodes.
while (!temp.empty()) {
l = temp.front();
temp.pop();
r = temp.front();
temp.pop();
if (l == NULL && r == NULL){
continue;
}
else if (l == NULL || r == NULL) {
return false;
}
else {
if (l->val != r->val) {
return false;
}
}
temp.push(l->left);
temp.push(r->right);
temp.push(l->right);
temp.push(r->left);
}
return true;
}