1490. Clone N-ary Tree

package LeetCode_1490

/**
 * 1490. Clone N-ary Tree
 * (Prime)
 * Given a root of an N-ary tree, return a deep copy (clone) of the tree.
Each node in the n-ary tree contains a val (int) and a list (List[Node]) of its children.
Nary-Tree input serialization is represented in their level order traversal, each group of children is separated by the null value (See examples).
Follow up: Can your solution work for the graph problem?
 * */
class Node(var `val`: Int) {
    var children: List<Node>? = null
}

class Solution {
    fun cloneTree(root: Node?): Node? {
        if (root == null) {
            return null
        }
        val copy = Node(root.`val`)
        val children = ArrayList<Node>()
        for (child in root.children!!) {
            val childClone = cloneTree(child)
            if (childClone != null) {
                children.add(childClone)
            }
        }
        copy.children = children
        return copy
    }
}

 

posted @ 2020-07-13 00:22  johnny_zhao  阅读(199)  评论(0编辑  收藏  举报