-
Notifications
You must be signed in to change notification settings - Fork 0
/
237.py
77 lines (56 loc) · 1.54 KB
/
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
"""
Problem:
A tree is symmetric if its data and shape remain unchanged when it is reflected about
the root node. The following tree is an example:
4
/ | \
3 5 3
/ \
9 9
Given a k-ary tree, determine whether it is symmetric.
"""
from typing import Dict, List
class Node:
def __init__(self, val: int) -> None:
self.val = val
self.children = []
def __str__(self) -> str:
return "{} -> {}".format(self.val, self.children)
def generate_tree_levels(
root: Node, levels: Dict[int, List[int]], level_number: int
) -> Dict[int, List[int]]:
# DFS to generate the nodes in the tree by level
if level_number not in levels:
levels[level_number] = []
levels[level_number].append(root.val)
for child in root.children:
generate_tree_levels(child, levels, level_number + 1)
return levels
def is_symmetric(node: Node) -> bool:
levels = generate_tree_levels(node, {}, 0)
# checking if the tree is symmetric
for level_values in levels.values():
if level_values != level_values[::-1]:
return False
return True
if __name__ == "__main__":
a = Node(4)
b = Node(5)
c = Node(3)
d = Node(3)
e = Node(9)
f = Node(9)
a.children = [c, b, d]
c.children = [f]
d.children = [e]
print(is_symmetric(a))
c.children = [f, Node(1)]
d.children = [Node(1), e]
print(is_symmetric(a))
c.val = 4
print(is_symmetric(a))
"""
SPECS:
TIME COMPLEXITY: O(n + e)
SPACE COMPLEXITY: O(n)
"""