很好的一道题,两个思路很经典。
/*
struct TreeNode {
int val;
struct TreeNode *left;
struct TreeNode *right;
TreeNode(int x) :
val(x), left(NULL), right(NULL) {
}
};
*/
//解法1:中序遍历看是不是前后对称
//解法2:如下
class Solution {
public:
bool isSymmetrical(TreeNode* pRoot)
{
if(pRoot == NULL)
return true;
return is(pRoot -> left, pRoot -> right);
}
bool is(TreeNode * l, TreeNode * r)
{
if(l == NULL && r == NULL)
return true;
if(l != NULL && r != NULL)
{
if(l -> val == r -> val)
{
return is(l -> left, r -> right) && is(l -> right, r -> left);
}
}
return false;
}
};