forked from kamyu104/LeetCode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
encode-n-ary-tree-to-binary-tree.py
66 lines (52 loc) · 1.64 KB
/
encode-n-ary-tree-to-binary-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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# Time: O(n)
# Space: O(h)
class Node(object):
def __init__(self, val, children):
self.val = val
self.children = children
# Definition for a binary tree node.
class TreeNode(object):
def __init__(self, x):
self.val = x
self.left = None
self.right = None
class Codec(object):
def encode(self, root):
"""Encodes an n-ary tree to a binary tree.
:type root: Node
:rtype: TreeNode
"""
def encodeHelper(root, parent, index):
if not root:
return None
node = TreeNode(root.val)
if index+1 < len(parent.children):
node.left = encodeHelper(parent.children[index+1], parent, index+1)
if root.children:
node.right = encodeHelper(root.children[0], root, 0)
return node
if not root:
return None
node = TreeNode(root.val)
if root.children:
node.right = encodeHelper(root.children[0], root, 0)
return node
def decode(self, data):
"""Decodes your binary tree to an n-ary tree.
:type data: TreeNode
:rtype: Node
"""
def decodeHelper(root, parent):
if not root:
return
children = []
node = Node(root.val, children)
decodeHelper(root.right, node)
parent.children.append(node)
decodeHelper(root.left, parent)
if not data:
return None
children = []
node = Node(data.val, children)
decodeHelper(data.right, node)
return node