-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathh431.py
63 lines (49 loc) · 1.52 KB
/
h431.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
"""
# Definition for a Node.
class Node:
def __init__(self, val=None, children=None):
self.val = val
self.children = children
"""
"""
# Definition for a binary tree node.
class TreeNode:
def __init__(self, x):
self.val = x
self.left = None
self.right = None
"""
class Codec:
# Encodes an n-ary tree to a binary tree.
def encode(self, root: 'Optional[Node]') -> Optional[TreeNode]:
if not root :
return None
newRoot = TreeNode(root.val)
# Same depth are right branch
prev = None
for child in root.children :
treeReplacement = self.encode(child) # left branch encode children
if not newRoot.left :
newRoot.left = treeReplacement
else :
prev.right = treeReplacement
prev = treeReplacement
return newRoot
# Decodes your binary tree to an n-ary tree.
def decode(self, data: Optional[TreeNode]) -> 'Optional[Node]':
if not data :
return None
outputNode = Node(data.val)
outputNode.children = []
# data.left,
# data.left.right,
# data.left.right.right,
# data.left.right.right.right, etc. = children
temp = data.left
while temp :
outputNode.children.append(self.decode(temp))
temp = temp.right
return outputNode
# Your Codec object will be instantiated and called as such:
# codec = Codec()
# codec.decode(codec.encode(root))