Problem 420708 · easy · Phase 04 Non-Linear Data Structures

Postorder Values

binary trees · postorder traversal · stack

Given the root of a binary tree, return the list of node values in postorder: for every node, its whole left subtree comes first, then its whole right subtree, then the node itself. An iterative solution is encouraged.

Examples

      1
     / \
    2   3
   / \   \
  4   5   6

Input:  root = build_tree([1, 2, 3, 4, 5, None, 6])
Output: [4, 5, 2, 6, 3, 1]
Input:  root = build_tree([7])
Output: [7]

Constraints

  • 0 <= number of nodes <= 2000
  • -10**4 <= node.val <= 10**4
  • Target complexity: O(n) time.

Goals

  • Produce the postorder sequence (left subtree, right subtree, node)
  • Relate postorder to a reversed modified preorder
  • Handle chains and the empty tree
Starting Python…