struct TreeNode {
int val;
struct TreeNode *left;
struct TreeNode *right;
TreeNode(int x) :
val(x), left(NULL), right(NULL) {
}
};
*/
class Solution {
public:
bool isSymmetrical(TreeNode* root) {
//即是判断左子树的右节点是否与右子树的左节点是否相等
if(!root) return true;
return dfs(root->left, root->right);
}
bool dfs(TreeNode* p1, TreeNode* p2){
//当p1或者p2为空时,需要两者均为空时才返回true,否则返回false
if(!p1 || !p2) return !p1 && !p2;
if(p1->val != p2->val) return false;
return dfs(p1->left, p2->right) && dfs(p1->right, p2->left);
}
};