Preorder Traversal (Recursive)

Learning Traversals DSA practice problem on Onlearn.

Difficulty: easy.

Topics: Recursive Preorder Traversal of a Binary Tree, Binary Tree, Traversal, Recursion, Stack, Depth-First Search, dfs, binary tree, recursion base case, preorder traversal, recursion, time complexity analysis, tree traversal.

Preorder Traversal of Binary Tree Problem Statement : Given the root of a binary tree, write a recursive function that returns an array containing the preorder traversal of the tree. Input Specification : The input represents a binary tree as a level order traversal (breadth first order) with 1 indicating null nodes. Example: 1 2 3 4 5 1 7 represents a tree where: Root = 1 (level 0) Left child = 2, Right child = 3 (level 1) Left child of 2 = 4, Right child of 2 = 5 (level 2) Left child of 3 = null, Right child of 3 = 7 (level 2) Output Specification : Return an array of integers representing the preorder traversal (Root → Left → Right). Constraints : The number of nodes in the tree is in the range [0, 10^4] 10^4 <= Node.val <= 10^4 Sample Input 1 : Sample Output 1 : Explanation : The preorder traversal processes nodes in the order: Root (4), left subtree (2→3→9→1), right subtree (5→7→6→8). Sample Input 2 : Sample Output 2 : Difficulty Level : Easy