[Kotlin] Simple O(n) Solution
\nclass Solution {\n    fun smallestMissingValueSubtree(parents: IntArray, nums: IntArray): IntArray {\n        val children = Array(parents.size) { mutableList
```\nclass Solution {\n    fun smallestMissingValueSubtree(parents: IntArray, nums: IntArray): IntArray {\n        val children = Array(parents.size) { mutableListOf<Int>() }.also {\n            for (i in 1..parents.lastIndex) {\n                it[parents[i]].add(i)\n            }\n        }\n\n        val res = IntArray(parents.size) { 1 }\n        nums.indexOfFirst { it == 1 }.let { startNode ->\n            var currentMissing = 1\n            var currentNode = startNode\n            var prevNode = -1\n            val values = mutableSetOf<Int>()\n            \n            fun addValuesUnder(node: Int) {\n                values.add(nums[node])\n                children[node].forEach { addValuesUnder(it) }\n            }\n            \n            while (currentNode != -1) {\n                values.add(nums[currentNode])\n                children[currentNode].forEach {\n                    if (it != prevNode) { addValuesUnder(it) }\n                }\n\n                while (values.contains(currentMissing)) {\n                    ++currentMissing\n                }\n\n                res[currentNode] = currentMissing\n                prevNode = currentNode\n                currentNode = parents[currentNode]\n            }\n        }\n\n        return res\n    }\n}\n```
['Kotlin']