Iterative Postorder Traversal (2 Stacks)
Learning Traversals DSA practice problem on Onlearn.
Difficulty: easy.
Topics: Iterative postorder traversal of a binary tree using two stacks, Binary Tree, Stack, Iterative Algorithm, Traversal, Postorder Traversal, complexity analysis, stack, binary tree, postorder traversal, tree traversal, Stack (LIFO), Iterative Tree Traversals.
Given the root of a binary tree, implement a function to perform a postorder traversal using exactly two stacks (without recursion) and return the traversal sequence as an array. The postorder traversal visits nodes in the order: left subtree → right subtree → root node. Input: Binary tree represented as a level order traversal array where null values are represented as 1 Examples: Input: [4,2,5,3, 1,7,6, 1,9, 1, 1,8, 1,1] Represents binary tree: 4 / \ 2 5 / \ / \ 3 1 7 6 / \ / \ 1 9 1 8 / \ 1 1 Output: [1,9,3,2,7,8,6,5,4] Input: [1,2,3,4,5,6,7, 1, 1,8, 1, 1, 1,9,10] Represents binary tree: 1 / \ 2 3 / \ / \ 4 5 6 7 \ / \ 8 9 10 Output: [4,8,5,2,6,9,10,7,3,1] Constraints: • The number of nodes in the tree is in the range [0, 104] • 1000 <= Node.val <= 1000