-
Notifications
You must be signed in to change notification settings - Fork 26
/
101.SymmetricTree.py
56 lines (50 loc) · 1.51 KB
/
101.SymmetricTree.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
53
54
55
56
"""
Given a binary tree, check whether it is a mirror of itself
(ie, symmetric around its center).
For example, this binary tree [1,2,2,3,4,4,3] is symmetric:
1
/ \
2 2
/ \ / \
3 4 4 3
But the following [1,2,2,null,3,null,3] is not:
1
/ \
2 2
\ \
3 3
Follow up: Solve it both recursively and iteratively.
"""
#Difficulty: Easy
#195 / 195 test cases passed.
#Runtime: 28 ms
#Memory Usage: 14.1 MB
#Runtime: 28 ms, faster than 95.44% of Python3 online submissions for Symmetric Tree.
#Memory Usage: 14.1 MB, less than 23.72% of Python3 online submissions for Symmetric Tree.
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
def isSymmetric(self, root: TreeNode) -> bool:
queue = [root]
while queue:
length = len(queue)
level = []
while length:
node = queue.pop(0)
level.append(node.val if node else None)
length -= 1
if node:
queue.append(node.left)
queue.append(node.right)
i = 0
j = len(level) - 1
while i <= j:
if level[i] != level[j]:
return False
i += 1
j -= 1
return True