AlgoFrames

Pre-order traversal

Standard · Write code · about 8 min

Given the root of a binary tree, return the values of its nodes in pre-order: the node, then the left subtree, then the right subtree.

Example 1

Input:

[[1,null,2,3]]

Output:

[1,2,3]

Visit 1, then 2, then the left child of 2, which is 3.

Example 2

Input:

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

Output:

[4,2,1,3,6]

Constraints

  • 0 ≤ number of nodes ≤ 1,000
  • -1,000 ≤ Node.val ≤ 1,000
  • The tree is given in level order; null marks a missing child

Learn the idea first: Pre-order, in-order, post-order, a visual lesson.

More Pre-order, in-order, post-order problems