Asked at

Binary Tree Preorder Traversal

Easy
Verified
TreeDFSStack~12 min

Given the root of a binary tree, return the preorder traversal of its node values: node, then left subtree, then right subtree.

A node is { val, left, right } with null children; the whole empty tree is null. The input arrives as the root node directly.

Examples

in{val:1, right:{val:2, left:{val:3}}}
out[1, 2, 3]

Node, then left subtree, then right subtree, applied recursively.

innull
out[]

An empty tree yields no values.

Constraints

  • 0 ≤ number of nodes ≤ 100
  • -100 ≤ node.val ≤ 100

Get help

🔑

Sign in to solve

Sign in to write, run, and submit your solution — and to pick up where your iOS flow left off.