Documentation IndexFetch the complete documentation index at: /llms.txtUse this file to discover all available pages before exploring further.
Fetch the complete documentation index at: /llms.txt
Use this file to discover all available pages before exploring further.
Tested Python solution for LeetCode 590 with 14 pytest cases. Generate a practice environment with lcpy.
lcpy gen -n 590 # by problem number lcpy gen -s n_ary_tree_postorder_traversal # by problem name
root
Input: root = [1,null,3,2,4,null,5,6] Output: [5,6,3,2,4,1]
Input: root = [1,null,2,3,4,5,null,null,6,7,null,8,null,9,10,null,null,11,null,12,null,13,null,null,14] Output: [2,6,14,11,7,3,12,8,4,13,9,10,5,1]
from __future__ import annotations class NaryNode: def __init__(self, val: int = 0, children: list[NaryNode] | None = None) -> None: self.val = val self.children = children if children is not None else [] class Solution: # Time: O(n) # Space: O(n) def postorder(self, root: NaryNode | None) -> list[int]: result: list[int] = [] if root is None: return result stack = [root] while stack: node = stack.pop() result.append(node.val) stack.extend(node.children) # Reverse preorder (children visited right-to-left) equals postorder. result.reverse() return result