题解 | #对称的二叉树#
对称的二叉树
https://www.nowcoder.com/practice/ff05d44dfdb04e1d83bdbdab320efbcb
/* struct TreeNode { int val; struct TreeNode *left; struct TreeNode *right; TreeNode(int x) : val(x), left(NULL), right(NULL) { } }; */ class Solution { public: bool ans(TreeNode *p1,TreeNode *p2){ if(p1==nullptr&&p2==nullptr){ return true; } else if(p1==nullptr||p2==nullptr){ return false; } if(ans(p1->right,p2->left)&&ans(p1->left,p2->right)){ if(p1->val==p2->val){ return true; } else{ return false; } } return false; } bool isSymmetrical(TreeNode* pRoot) { if(pRoot==nullptr){ return true; } return ans(pRoot->right,pRoot->left); } };