-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path101.对称二叉树.py
More file actions
70 lines (62 loc) · 1.38 KB
/
101.对称二叉树.py
File metadata and controls
70 lines (62 loc) · 1.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#
# @lc app=leetcode.cn id=101 lang=python
#
# [101] 对称二叉树
#
# https://leetcode-cn.com/problems/symmetric-tree/description/
#
# algorithms
# Easy (48.79%)
# Likes: 473
# Dislikes: 0
# Total Accepted: 63.3K
# Total Submissions: 129.8K
# Testcase Example: '[1,2,2,3,4,4,3]'
#
# 给定一个二叉树,检查它是否是镜像对称的。
#
# 例如,二叉树 [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
#
#
# 说明:
#
# 如果你可以运用递归和迭代两种方法解决这个问题,会很加分。
#
#
# @lc code=start
# Definition for a binary tree node.
# class TreeNode(object):
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution(object):
def isSymmetric(self, root):
"""
:type root: TreeNode
:rtype: bool
"""
return self.compare(root,root)
def compare(self,root1,root2):
if root1==None and root2==None:
return True
elif root1!=None and root2!=None and root1.val==root2.val:
return self.compare(root1.left,root2.right) and self.compare(root1.right,root2.left)
else:
return False
# @lc code=end