Binary Tree Preorder Traversal

Given the root of a binary tree, return the preorder traversal of its nodes' values. Pattern focus: Preorder DFS (Root First). Visit root, then left subtree, then right subtree.

Input Format

root = binary tree root

Output Format

preorder traversal as an array of node values

Constraints

  • 1 <= number of nodes <= 10^5
  • -10^4 <= node values <= 10^4
  • Input must satisfy the format described in inputFormat.

Examples

Example 1:

Input:

root = [1,null,2,3]

Output:

[1,2,3]

Explanation:

Preorder visits the root before its children.

Example 2:

Input:

root = [1,2,3,4,5,null,6]

Output:

[1,2,4,5,3,6]

Explanation:

The traversal follows root-left-right order.

Loading...
Binary Tree Preorder Traversal - Binary Tree