"""
# Definition for a Node.
class Node:
def __init__(self, val=None, children=None):
self.val = val
self.children = children
"""
class Solution:
def postorder(self, root: 'Node') -> List[int]:
stack, ans = [root], []
while stack:
if (obj := stack.pop()) is not None:
if type(obj) == Node:
stack += [obj.val] + obj.children[::-1]
else:
ans.append(obj)
return ans
"""
# Definition for a Node.
class Node:
def __init__(self, val=None, children=None):
self.val = val
self.children = children
"""
class Solution:
def postorder(self, root: 'Node') -> List[int]:
result = []
def post_order(root):
if root:
for node in root.children:
post_order(node)
result.append(root.val)
post_order(root)
return result
/**
* Definition for a Node.
* type Node struct {
* Val int
* Children []*Node
* }
*/
func postorder(root *Node) []int {
var res = []int{}
if root == nil{
return res
}
var st = []*Node{root}
for len(st) > 0 {
x := st[len(st)-1]
st = st[:len(st)-1]
if x != nil {
res = append(res, x.Val)
st = append(st, x.Children...)
}
}
n := len(res)
for i := 0; i < n/2; i++ {
res[i], res[n-1-i] = res[n-1-i], res[i]
}
return res
}