bugfree Icon
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course

Leetcode Problem 145. Binary Tree Postorder Traversal

145. Binary Tree Postorder Traversal

Leetcode Solutions

Iterative Postorder Traversal Using Two Stacks

  1. Initialize two empty stacks, stack1 and stack2.
  2. Push the root node onto stack1 if it is not null.
  3. While stack1 is not empty: a. Pop a node from stack1 and push it onto stack2. b. Push the left child of the popped node onto stack1 if it exists. c. Push the right child of the popped node onto stack1 if it exists.
  4. After the loop, stack2 will contain the nodes in postorder sequence. Pop all nodes from stack2 and add them to the output list.
  5. Return the output list.
UML Thumbnail

Recursive Postorder Traversal

Ask Question

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

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...
bugfree Icon
OR