这个题目和之前的 二叉树的镜像 非常的相似.
就是除根节点,左右子树要对称.
cpp
/**
* 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;
return dfs(root->right,root->left);
}
bool dfs(TreeNode *p,TreeNode *q){
if(!p || !q)return !p && !q;
if(p->val != q->val)return false;
return dfs(p->left,q->right) && dfs(p->right,q->left);
}
};
原文:https://www.cnblogs.com/Rowry/p/14409194.html