-
Notifications
You must be signed in to change notification settings - Fork 0
/
symmetric-tree.txt
31 lines (27 loc) · 957 Bytes
/
symmetric-tree.txt
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
# Definition for a binary tree node
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution:
# @param root, a tree node
# @return a boolean
def isSymmetric(self, root):
if root == None:
return True
self.flipTree(root.right)
return self.isTheSame(root.left, root.right)
def flipTree(self, root):
if root != None:
temp = root.left
root.left = root.right
root.right = temp
self.flipTree(root.left)
self.flipTree(root.right)
def isTheSame(self, p, q):
if (p == None and q != None) or (p != None and q == None) or (p != None and q != None and p.val != q.val):
return False
elif p == None and q == None:
return True
return self.isTheSame(p.left, q.left) and self.isTheSame(p.right, q.right)