Golang | Leetcode Golang题解之第430题扁平化多级双向链表
题目:
题解:
func dfs(node *Node) (last *Node) {cur := nodefor cur != nil {next := cur.Next// 如果有子节点,那么首先处理子节点if cur.Child != nil {childLast := dfs(cur.Child)next = cur.Next// 将 node 与 child 相连cur.Next = cur.Childcur.Child.Prev = cur// 如果 next 不为空,就将 last 与 next 相连if next != nil {childLast.Next = nextnext.Prev = childLast}// 将 child 置为空cur.Child = nillast = childLast} else {last = cur}cur = next}return
}func flatten(root *Node) *Node {dfs(root)return root
}