forked from ngiengkianyew/daily-coding-problem
-
Notifications
You must be signed in to change notification settings - Fork 0
/
problem_237.py
44 lines (33 loc) · 811 Bytes
/
problem_237.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
class Node:
def __init__(self, val):
self.val = val
self.children = list()
def __repr__(self):
return "{} -> {}".format(self.val, self.children)
def update_levels_dict(root, levels, lnum):
if lnum not in levels:
levels[lnum] = list()
levels[lnum].append(root.val)
for child in root.children:
update_levels_dict(child, levels, lnum + 1)
def is_symmetric(tree):
levels = dict()
update_levels_dict(tree, levels, 0)
for level in levels:
arr = levels[level]
if arr != arr[::-1]:
return False
return True
# Tests
e = Node(9)
f = Node(9)
d = Node(3)
d.children = [e]
c = Node(3)
c.children = [f]
b = Node(5)
a = Node(4)
a.children = [c, b, d]
assert is_symmetric(a)
c.val = 4
assert not is_symmetric(a)