Leetcode Problem 144. Binary Tree Preorder Traversal

144. Binary Tree Preorder Traversal

Leetcode Solutions

Iterative Preorder Traversal

  1. Initialize an empty stack, stack, and an output list, result.
  2. If the root is not null, push the root onto the stack.
  3. While the stack is not empty: a. Pop the top node from the stack and add its value to result. b. If the popped node has a right child, push it onto the stack. c. If the popped node has a left child, push it onto the stack.
  4. Return the result list containing the preorder traversal.
UML Thumbnail

Recursive Preorder Traversal

Ask Question

Programming Language
image/screenshot of info(optional)
Full Screen
Loading...

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...