15% off all plans & roadmaps Use code
See plansProblem editorial
A binary tree is a structure in which each node has a value and up to two children: a left child and a right child. root refers to the top node of the tree; a missing child is represented by null in the level-order input, and an empty tree is represented by [].
Binary Tree Preorder Traversal is a Tree Traversal Patterns (DFS & BFS) problem, in the Recursive Preorder Traversal subpattern. The pattern covers preorder, inorder, level order traversal, LCA, and serialization
The reference solution runs in O(n), where n is the number of nodes in the tree. time and O(h) auxiliary space for the recursion stack, where h is the tree height; O(n) including the returned result. space.
The full editorial is part of Pro
It explains the core insight, the complexity analysis and the common mistakes, with a worked solution.
Binary Tree Preorder Traversal is a Tree Traversal Patterns (DFS & BFS) problem, in the Recursive Preorder Traversal subpattern. Once the approach above makes sense, write it yourself and then work through the neighbouring problems that reuse it.