Problems/215. Binary Tree Preorder Traversal

215. Binary Tree Preorder Traversal

EasyBinary Tree

Given the root of a binary tree, visit and collect its node values using a preorder traversal strategy (visiting the current node first, then recursively traversing the left subtree, and finally traversing the right subtree).

Example 1
Input: root = [4, 2, 6, 1, 3, 5, 7]
Output: [4, 2, 1, 3, 6, 5, 7]
We visit the root (4) first, then recursively traverse its left child (2) and its children (1, 3), and then visit the right child (6) and its children (5, 7).
Example 2
Input: root = [1, null, 2, null, 3]
Output: [1, 2, 3]
We visit the root (1) first, then traverse its right child (2), visiting it, and then its right child (3), visiting it.
Example 3
Input: root = [1]
Output: [1]
Only the root node is present, so we visit the root value 1 and return.
Visualizer

Visualizer will appear here