Binary Tree Preorder Traversal - aloalgo

Binary Tree Preorder Traversal

Easy

You are given the root of a binary tree, return the preorder traversal of its nodes' values.

In a preorder traversal, you visit the current node first, then recursively traverse the left subtree, and finally recursively traverse the right subtree.

Example 1

Input
12453
Output
[1, 2, 4, 5, 3]
Explanation:

The traversal order is: 1 (root) -> 2 (left) -> 4 (left's left) -> 5 (left's right) -> 3 (right).

Example 2

Input
None
Output
[]
Explanation:

An empty tree results in an empty traversal.

Example 3

Input
123
Output
[1, 2, 3]
Explanation:

The traversal order is: 1 (root) -> 2 (right) -> 3 (right's right).

Loading...
Input
12453
Output
[1, 2, 4, 5, 3]

Hello! I am your ✨ AI assistant. I can provide you hints, explanations, give feedback on your code, and more. Just ask me anything related to the problem you're working on!