Решение
class Solution {
fun postorder(root: Node?): List<Int> {
if (root == null) return emptyList()
var cap = 1024
var stack = arrayOfNulls<Node>(cap)
var idx = IntArray(cap)
var top = 0
fun push(x: Node) {
if (top == cap) {
val ncap = cap shl 1
val ns = arrayOfNulls<Node>(ncap)
System.arraycopy(stack, 0, ns, 0, cap)
stack = ns
idx = idx.copyOf(ncap)
cap = ncap
}
stack[top] = x
idx[top] = 0
top++
}
push(root)
val res = ArrayList<Int>()
while (top > 0) {
val t = top - 1
val node = stack[t]!!
val i = idx[t]
val ch = node.children
if (i < ch.size) {
idx[t] = i + 1
val c = ch[i]
if (c != null) push(c)
} else {
res.add(node.`val`)
stack[t] = null
top--
}
}
return res
}
}