C# - Accumulate children values from node with value 1 to all of its ancestor
csharp\npublic int[] SmallestMissingValueSubtree(int[] parents, int[] nums)\n{\n\tint n = parents.Length;\n\tList<int>[] children = new List<int>[n];\n\tint[] r
```csharp\npublic int[] SmallestMissingValueSubtree(int[] parents, int[] nums)\n{\n\tint n = parents.Length;\n\tList<int>[] children = new List<int>[n];\n\tint[] result = new int[n];\n\tArray.Fill(result, 1);\n\tint indexWithValueOne = -1;\n\n\tif (nums[0] == 1)\n\t{\n\t\tindexWithValueOne = 0;\n\t}\n\n\tfor (int i = 1; i < n; i++)\n\t{\n\t\tint parent = parents[i];\n\t\tif (children[parent] == null)\n\t\t{\n\t\t\tchildren[parent] = new List<int>() { i };\n\t\t}\n\n\t\telse\n\t\t{\n\t\t\tchildren[parent].Add(i);\n\t\t}\n\n\t\tif (nums[i] == 1)\n\t\t{\n\t\t\tindexWithValueOne = i;\n\t\t}\n\t}\n\n\tif (indexWithValueOne == -1)\n\t{\n\t\treturn result;\n\t}\n\n\tHashSet<int> values = new HashSet<int>();\n\tint start = indexWithValueOne;\n\tbool[] visited = new bool[n];\n\tint next = 2;\n\n\twhile (start != -1)\n\t{\n\t\tvalues.Add(nums[start]);\n\t\tgetchildValues(start, children, nums, visited, values);\n\n\t\twhile (values.Contains(next))\n\t\t{\n\t\t\tnext++;\n\t\t}\n\n\t\tresult[start] = next;\n\t\tstart = parents[start];\n\t}\n\n\treturn result;\n}\n\nprivate void getchildValues(int index, List<int>[] children, int[] nums, bool[] visited, HashSet<int> values)\n{\n\tif (children[index] == null)\n\t{\n\t\treturn;\n\t}\n\n\tforeach (var child in children[index])\n\t{  \n\t\tif(!visited[child])\n\t\t{\n\t\t\tvalues.Add(nums[child]);\n\t\t\tgetchildValues(child, children, nums, visited, values);\n\t\t\tvisited[child] = true;\n\t\t}\n\t}\n\n\tvisited[index] = true;\n}\n```
[]