/**
* 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 == NULL)
return true;
return isMirrorTree(root->left, root->right);
}
bool isMirrorTree(TreeNode* p, TreeNode* q) {
if(p == NULL && q == NULL)
return true;
if((p == NULL && q != NULL) || (p != NULL && q == NULL))
return false;
if(p->val == q->val){
if(isMirrorTree(p->left, q->right) && isMirrorTree(p->right, q->left))
return true;
else
return false;
}
else
return false;
}
};leetcode-100 改版,把递归条件修改,改为检测镜像树即可。我真是机智
leetcode-101-symmetric tree
最新推荐文章于 2022-06-04 21:05:23 发布
本文介绍了一种判断二叉树是否为镜像对称的方法。通过递归比较左右子树来实现,若左右子树完全镜像则返回真值。此方法适用于计算机科学中的数据结构与算法领域。
217

被折叠的 条评论
为什么被折叠?



