"""
# Definition for a Node.
class Node(object):
def __init__(self, val=None, children=None):
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:
def encode(self, root):
"""Encodes an n-ary tree to a binary tree.
"""
if not root:
return None
rootNode = TreeNode(root.val)
queue = deque([(rootNode, root)])
while queue:
parent, curr = queue.popleft()
prevBNode = None
headBNode = None
# traverse each child one by one
for child in curr.children:
newBNode = TreeNode(child.val)
if prevBNode:
prevBNode.right = newBNode
else:
headBNode = newBNode
prevBNode = newBNode
queue.append((newBNode, child))
# use the first child in the left node of parent
parent.left = headBNode
return rootNode
def decode(self, data):
"""Decodes your binary tree to an n-ary tree.
"""
if not data:
return None
# should set the default value to [] rather than None,
# otherwise it wont pass the test cases.
rootNode = Node(data.val, [])
queue = deque([(rootNode, data)])
while queue:
parent, curr = queue.popleft()
firstChild = curr.left
sibling = firstChild
while sibling:
# Note: the initial value of the children list should not be None, which is assumed by the online judge.
newNode = Node(sibling.val, [])
parent.children.append(newNode)
queue.append((newNode, sibling))
sibling = sibling.right
return rootNodeTime complexity:
Space complexity:
encode() : .
decode() : .
Since is proportional to in the worst case, we could further generalize the space complexity to .
Where is the number of nodes in the N-ary tree, and is the maximum number of nodes that reside at the same level.
"""
# Definition for a Node.
class Node(object):
def __init__(self, val=None, children=None):
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:
def encode(self, root):
"""Encodes an n-ary tree to a binary tree.
"""
if not root:
return None
rootNode = TreeNode(root.val)
if len(root.children) > 0:
firstChild = root.children[0]
rootNode.left = self.encode(firstChild)
# the parent for the rest of the children
curr = rootNode.left
# encode the rest of the children
for i in range(1, len(root.children)):
curr.right = self.encode(root.children[i])
curr = curr.right
return rootNode
def decode(self, data):
"""Decodes your binary tree to an n-ary tree.
"""
if not data:
return None
rootNode = Node(data.val, [])
curr = data.left
while curr:
rootNode.children.append(self.decode(curr))
curr = curr.right
return rootNodeTime complexity:
Space complexity:
Since is proportional to in the worst case, we could further generalize the space complexity to
Unlike the BFS algorithm, we don't use the queue data structure in the DFS algorithm. However, implicitly the algorithm would consume more space in the function call stack due to the recursive function calls.
And this consumption of call stack space is the main space complexity for our DFS algorithm. As we can see, the size of the call stack at any moment is exactly the number of level where the currently visited node resides, e.g. for the root node (level 0), the recursive call stack is empty.
Where is the number of nodes in the N-ary tree, and is the depth of the N-ary tree.