You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository has been archived by the owner on Jul 30, 2020. It is now read-only.
if root.children is None or len(root.children) <= 0:
return binary
binary.right = self.encode(root.children[0]) # Right child of binary is the encoding of of all n-ary children, starting with the first child. Other children of n-array root are left child of previous nodes child.
node = binary.right
for child in root.children[1:]:
node.left = self.encode(child)
node = node.left
return binary
def decode(self, data):
"""Decodes your binary tree to an n-ary tree.
:type data: TreeNode
:rtype: Node
"""
if data is None:
return None
n_ary = Node(data.val, []) # Create n-ary root
node = data.right # Move to first child of n-ary root
while node: # while more children of n-ary root
n_ary.children.append(self.decode(node))
node = node.left
return n_ary
# Your Codec object will be instantiated and called as such: