class Solution {
public:
int x=1;
void lh(TreeNode* r,TreeNode* l)
{
if(r&&l)
{
if(r->val!=l->val)
{
x=0;
}
lh(r->left,l->right);
lh(r->right,l->left);
}
else if(r&&!l||!r&&l)
{
x=0;
}
}
bool isSymmetric(TreeNode* root) {
if(root->left&&root->right)
{
TreeNode* r=root->right;
TreeNode* l=root->left;
lh(r,l);
}
else if(!root->left&&!root->right)
{
return 1;
}
else
{
return 0;
}
return x;
}
};
递归...百感交集...
加油!!!
不过答案好看多了
class Solution {
public:
bool check(TreeNode *p, TreeNode *q) {
if (!p && !q) return true;
if (!p || !q) return false;
return p->val == q->val && check(p->left, q->right) && check(p->right, q->left);
}
bool isSymmetric(TreeNode* root) {
return check(root, root);
}
};
迭代有点像广度优先。可以熟悉熟悉。