430. Flatten a Multilevel Doubly Linked List --Code in the post

Hi, I have a question abut 430. Flatten a Multilevel Doubly Linked List.

One way I know is to insert the child linked list after the node if it has one, and then keeps iterating the list, and it will eventually flatten the whole list. But I was told and agreed that it could also be treated as a tree. It can be done by generating a list using DFS and then creating a doubled linked connection between the nodes. I implemented this in python3.

I got the right order in terms of the node's values. But it seemed the $ref to the previous node was ruined somehow, so it can't be accepted. Can someone please kindly let me know what I did wrong? Thank you very much.

"""
# Definition for a Node.
class Node:
    def __init__(self, val, prev, next, child):
        self.val = val
        self.prev = prev
        self.next = next
        self.child = child
"""
class Solution:
    def flatten(self, head: 'Node') -> 'Node':
        if not head:
            return None
        if not head.child and not head.next:
            return head
        res = [head]
        node = head
		
		# dfs, add nodes to a list
        def helper(node):
            while node:
                if node.child:
                    res.append(node.child)
                    helper(node.child)
                if node.next:
                    res.append(node.next)
                node = node.next
                
        helper(node)        
		# add pre, next for all nodes in the flattened list, and make child points to null 
        i = 1
        head.pre = None
        head.next = res[1]
        while i < len(res) -1:
            res[i].pre = res[i-1]
            res[i].next = res[i+1]
            res[i].child = None
            i += 1
        #here is the last node i == len(res) - 1:
        res[i].pre = res[i-1]
        res[i].next = None
        res[i].child = None
        return head

The anwer is not accepted showing below differences, any comments regarding the solution or coding style or anthing is greatly appreciated. Thanks.

output
{"id":"1","child":null,"next":{"id":"2","child":null,"next":{"id":"3","child":null,"next":{"id":"4","child":null,"next":{"id":"5","child":null,"next":{"id":"6","child":null,"next":{"id":"7","child":null,"next":{"id":"8","child":null,"next":{"id":"9","child":null,"next":{"id":"10","child":null,"next":{"id":"11","child":null,"next":{"id":"12","child":null,"next":null,"prev":{"ref":"11"},"val":6},"prev":{"ref":"10"},"val":5},"prev":{"ref":"3**"},"val":4},"prev":{"ref":"8"},"val":10},"prev":{"ref":"5**"},"val":9},"prev":{"ref":"6"},"val":12},"prev":null,"val":11},"prev":{"ref":"4"},"val":8},"prev"**:null,**"val":7},"prev":{"ref":"2"},"val":3},"prev":{"$ref":"1"},"val":2},"prev":null,"val":1}

expected:
{"id":"1","child":null,"next":{"id":"2","child":null,"next":{"id":"3","child":null,"next":{"id":"4","child":null,"next":{"id":"5","child":null,"next":{"id":"6","child":null,"next":{"id":"7","child":null,"next":{"id":"8","child":null,"next":{"id":"9","child":null,"next":{"id":"10","child":null,"next":{"id":"11","child":null,"next":{"id":"12","child":null,"next":null,"prev":{"ref":"11"},"val":6},"prev":{"ref":"10"},"val":5},"prev":{"ref":"9"**},"val":4},"prev":{"ref":"8"},"val":10},"prev":{"ref":"7"**},"val":9},"prev":{"ref":"6"},"val":12},"prev":{"ref":"5**"},"val":11},"prev":{"ref":"4"},"val":8},"prev":{"ref":"3"**},"val":7},"prev":{"ref":"2"},"val":3},"prev":{"$ref":"1"},"val":2},"prev":null,"val":1}

Comments (0)