zoukankan      html  css  js  c++  java
  • 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
        }
    }
  • 相关阅读:
    uva129
    uva167
    【Linux】touch命令
    【Linux】od命令
    【Linux】tail命令
    【Linux】head命令
    【Linux】less命令
    【Linux】more命令
    【Linux】nl命令
    【Linux】cat命令
  • 原文地址:https://www.cnblogs.com/johnnyzhao/p/13290896.html
Copyright © 2011-2022 走看看