-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path2172_isSymmetric.py
52 lines (50 loc) · 1.55 KB
/
2172_isSymmetric.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
43
44
45
46
47
48
49
50
51
52
# Definition for a binary tree node.
# class TreeNode(object):
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
from copy import copy
class Solution(object):
def isSymmetric(self, root):
"""
:type root: TreeNode
:rtype: bool
"""
if root is None :
return True
clone_root=self.clone(root)
mirror=self.mirrorTree(clone_root)
return self.comp(mirror,root)
def clone(self,root):
clone_root=copy(root)
if root.left is not None:
clone_root.left=self.clone(root.left)
if root.right is not None:
clone_root.right=self.clone(root.right)
return clone_root
def mirrorTree(self, root):
"""
:type root: TreeNode
:rtype: TreeNode
"""
if root is None:
return root
self.swap(root)
return root
def swap(self,node):
if(node.left is not None):
self.swap(node.left)
if(node.right is not None):
self.swap(node.right)
if node.left is not None or node.right is not None:
temp=node.left
node.left=node.right
node.right=temp
def comp(self,A,B):
if A is None and B is None :return True
if (A is None and B is not None) or (A is not None and B is None):return False
if A.val != B.val : return False
if not self.comp(A.left,B.left): return False
if not self.comp(A.right,B.right): return False
return True