forked from DmrfCoder/AlgorithmAndDataStructure
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path63.py
42 lines (34 loc) · 932 Bytes
/
63.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
37
38
39
40
41
42
# -*- coding:utf-8 -*-
class TreeNode:
def __init__(self, x):
self.val = x
self.left = None
self.right = None
class Solution:
def isSymmetrical(self, pRoot):
if pRoot is None:
return True
return self.cmpRoot(pRoot.left, pRoot.right)
def cmpRoot(self, root1, root2):
if root1 == None and root2 == None:
return True
elif root1 == None or root2 == None:
return False
if root1.val != root2.val:
return False
return self.cmpRoot(root1.left, root2.right) and self.cmpRoot(root1.right, root2.left)
s = Solution()
pRoot = TreeNode(8)
node1 = TreeNode(6)
node2 = TreeNode(6)
node3 = TreeNode(5)
node4 = TreeNode(7)
node5 = TreeNode(7)
node6 = TreeNode(5)
pRoot.left = node1
pRoot.right = node2
node1.left = node3
node1.right = node4
node2.left = node5
node2.right = node6
print s.isSymmetrical(pRoot)