Scala, very clean and well organized code
scala\n  def smallestMissingValueSubtree(parents: Array[Int], nums: Array[Int]): Array[Int] = {\n    type int = Int\n    type ai  = Array[int]\n    import scala
```scala\n  def smallestMissingValueSubtree(parents: Array[Int], nums: Array[Int]): Array[Int] = {\n    type int = Int\n    type ai  = Array[int]\n    import scala.collection.mutable.{HashMap, HashSet}\n    type hs = HashSet[int]\n    val graph = new HashMap[int, hs]\n    val n     = nums.length\n    parents.zipWithIndex.foreach {\n      case (par, self) => {\n        if (!graph.contains(self)) graph += (self            -> new hs)\n        if (!graph.contains(par) && par != -1) graph += (par -> new hs)\n        if (par != -1) graph(par) += self\n      }\n    }\n\n    val res = Array.fill[int](n)(1)\n    import scala.util.control.Breaks._\n    var idOne = -1\n    breakable {\n      while (idOne < n - 1) {\n        if (nums(idOne + 1) != 1) idOne += 1 else { idOne += 1; break() }\n      }\n    }\n    if (idOne == -1) return res\n    val isv      = new hs\n    val valueSet = new hs\n    var min      = 1\n\n    def f(root: int): Unit = {\n      if (isv.contains(root)) return\n      isv += root\n      valueSet += nums(root)\n      graph(root).foreach(ch => if (!isv.contains(ch)) f(ch))\n\n    }\n\n    while (idOne != -1) {\n      f(idOne)\n      while (valueSet.contains(min)) min += 1\n      res(idOne) = min\n      idOne = parents(idOne)\n    }\n\n    res\n  }\n```
['Scala']