Search⌘ K
AI Features

Solution: Binary Tree Preorder Traversal

Explore how to perform a preorder traversal of a binary tree using an optimized depth-first search that avoids recursion or extra memory. Understand how to temporarily reuse tree pointers to visit nodes root-left-right while restoring the tree structure. This lesson explains the technique step-by-step and helps you grasp the algorithm's linear time complexity and constant space usage.

Statement

Given the root of a binary tree, your task is to return a list containing the values of its nodes in preorder traversalIn preorder traversal, we visit each node by first visiting the current node, then its left subtree, and finally its right subtree order.

Constraints:

  • The number of nodes in the tree is in the range [0,100] ...