-
Notifications
You must be signed in to change notification settings - Fork 0
/
101.py
27 lines (25 loc) · 959 Bytes
/
101.py
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
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
def check_left_equals_to_right(self, left, right):
if not left and not right:
# 最終的節點
return True
elif not (left and right):
# 缺左邊or缺右邊
return False
else:
if left.val != right.val:
ans = False
else:
# 要檢查左樹=右樹
ans = self.check_left_equals_to_right(left=left.left, right=right.right)
if ans:
ans = self.check_left_equals_to_right(left=left.right, right=right.left)
return ans
def isSymmetric(self, root: Optional[TreeNode]) -> bool:
return self.check_left_equals_to_right(left=root.left, right=root.right)