Search⌘ K
AI Features

Solution: Binary Tree Preorder Traversal

Explore how to implement preorder traversal of a binary tree using depth-first search without recursion or extra stack space. This lesson teaches the Morris traversal technique to visit nodes in root-left-right order by temporarily modifying tree pointers, optimizing space to O 1 while maintaining O n time complexity.

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] ...