Skip to content

[LeetCode] 101. 对称二叉树 #28

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
Animenzzzz opened this issue Aug 7, 2019 · 0 comments
Open

[LeetCode] 101. 对称二叉树 #28

Animenzzzz opened this issue Aug 7, 2019 · 0 comments

Comments

@Animenzzzz
Copy link
Owner

题目描述:

给定一个二叉树,检查它是否是镜像对称的。

例如,二叉树 [1,2,2,3,4,4,3] 是对称的。

    1
   / \
  2   2
 / \ / \
3  4 4  3

但是下面这个 [1,2,2,null,3,null,3] 则不是镜像对称的:

    1
   / \
  2   2
   \   \
   3    3

说明:

如果你可以运用递归和迭代两种方法解决这个问题,会很加分。

解题思路:两棵树左右对称,两个根节点的值相同,根的左子树与右子树互为镜像。看的题目下的官网解题思路。

C解题:

bool isMirro(struct TreeNode* p, struct TreeNode* q){
    if(!p && !q) return true;
    if(!p || !q) return false;
    return (p->val == q->val) && isMirro(p->left,q->right) && isMirro(p->right,q->left);
}

bool isSymmetric(struct TreeNode* root){
    return isMirro(root,root);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant