Comment on page

# 101.Symmetric Tree

101.Symmetric Tree

1
/ \
2 2
/ \ / \
3 4 4 3

1
/ \
2 2
\ \
3 3

/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
bool check(TreeNode* left , TreeNode* right)
{
if(!left && !right)return true;
else if( !left || !right) return false;
if(left->val != right->val) return false;
return check(left->right, right->left) && check(left->left, right->right);
}
public:
bool isSymmetric(TreeNode* root) {
if(!root) return true;
if(!root->left && !root->right) return true;
return check(root->left, root->right);
}
};

/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
bool isSymmetric(TreeNode* root) {
if(!root) return true;
if(!root->left && !root->right) return true;
deque<TreeNode*> t;
t.push_back(root);
t.push_back(root);
while(!t.empty())
{
if(!t[0] &&!t[1])
{
t.pop_front();
t.pop_front();
continue;
}
if(!t[0] || !t[1]) return false;
if(t[0]->val != t[1]->val) return false;
t.push_back(t[0]->left);
t.push_back(t[1]->right);
t.push_back(t[0]->right);
t.push_back(t[1]->left);
t.pop_front();
t.pop_front();
}
return true;
}
};