Search⌘ K
AI Features

Solution: Binary Tree Preorder Traversal

Discover how to efficiently traverse a binary tree in preorder by visiting root, left, then right nodes without recursion or stack. Learn to apply a Morris traversal technique that reuses pointers to optimize space to O(1), and understand its O(n) time complexity. This lesson helps you grasp a practical depth-first search strategy ideal for coding interviews.

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][0, 100].

  • 100-100 \leq ...