Binary Tree Postorder Traversal

Given the root of a binary tree, return the postorder traversal of its nodes' values.

Follow up: Recursive solution is trivial, could you do it iteratively?

Example 1
        1
         \
          2
         /
        3
Inputroot = [1,null,2,3]
Output[3,2,1]
Postorder traversal visits the left subtree, then the right subtree, then the root, producing [3, 2, 1].
Example 2
          1
         / \
        2   3
       / \   \
      4   5   8
         / \ /
        6  7 9
Inputroot = [1,2,3,4,5,null,8,null,null,6,7,9]
Output[4,6,7,5,2,9,8,3,1]
Visiting each subtree in postorder produces [4, 6, 7, 5, 2, 9, 8, 3, 1].

Constraints

  • The number of the nodes in the tree is in the range [0, 100].
  • -100 <= Node.val <= 100

Asked at 5 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate