-
Notifications
You must be signed in to change notification settings - Fork 7
/
symmetric_tree.py
40 lines (32 loc) · 1.04 KB
/
symmetric_tree.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
# Symmetric Tree
class TreeNode:
def __init__(self, val=0, left=None, right=None):
self.val = val
self.left = left
self.right = right
class Solution(object):
def isSymmetric(self, root):
queue = [root.left, root.right]
while queue:
left_node = queue.pop(0)
right_node = queue.pop(0)
if not left_node and not right_node:
continue
if not left_node or not right_node:
return False
if left_node.val != right_node.val:
return False
queue.append(left_node.left)
queue.append(right_node.right)
queue.append(left_node.right)
queue.append(right_node.left)
return True
root = TreeNode(1)
root.left = TreeNode(2)
root.right = TreeNode(2)
root.left.left = TreeNode(3)
root.left.right = TreeNode(4)
root.right.left = TreeNode(4)
root.right.right = TreeNode(3)
solution = Solution()
print(solution.isSymmetric(root))