-
Notifications
You must be signed in to change notification settings - Fork 1
/
101. 对称二叉树.py
36 lines (35 loc) · 1.09 KB
/
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
28
29
30
31
32
33
34
35
36
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution:
def isSymmetric(self, root: TreeNode) -> bool:
result1=[]
result2=[]
def my(flag,tree):
if flag:
if tree!=None:
result1.append(tree.val)
my(flag,tree.left)
my(flag,tree.right)
else:
result1.append(None)
else:
if tree!=None:
result2.append(tree.val)
my(flag,tree.right)
my(flag,tree.left)
else:
result2.append(None)
if root==None or (root.left==None and root.right==None):
return True
if root.left!=None and root.right!=None:
my(1,root.left)
my(0,root.right)
# print(result1, " ",result2)
return result1==result2
else:
return False
# 检测遍历结果就可以了