Iterative Preorder Traversal
Learning Traversals DSA practice problem on Onlearn.
Difficulty: easy.
Topics: Iterative Preorder Traversal of a Binary Tree using a Stack, Binary Tree, Traversal, Stack, Iterative Algorithm, Preorder Traversal, space complexity, dfs, stack, binary tree, preorder traversal, time complexity analysis, traversal, Iterative Tree Traversals.
Problem Statement Given the root of a binary tree, return the preorder traversal of its nodes' values. Preorder traversal visits the nodes in the following order: 1. Visit the Root node. 2. Traverse the Left subtree. 3. Traverse the Right subtree. Note: The goal is to implement this traversal iteratively (using an explicit stack) rather than recursively. Input Specification The input defines a binary tree using a level order traversal array representation (where null represents a missing node). In a function signature context, the input is the root node of the binary tree. Output Specification Return an array or list of integers representing the preorder traversal sequence of the tree. Constraints The number of nodes in the tree is in the range [0, 1000]. Node values are integers between 1000 and 1000. The height of the tree is reasonable for an iterative stack solution. Sample Test Cases Sample Input 1 (Explanation: The tree structure is: 1 is root, no left child, right child is 2. Node 2 has left child 3.) Sample Output 1 Sample Input 2 (Explanation: The tree structure is: 1 is root, left child 2, right child 3. Node 2 has left child 4 and right child 5.) Sample Output 2 Sample Input 3 Sample Output 3 Difficulty Level Easy