/145. Binary Tree Postorder Traversal

145. Binary Tree Postorder Traversal

Easy
Stack77.7% acceptance

Given the root node of a binary tree, return a list containing the values of the nodes in postorder traversal order. Postorder traversal visits the left subtree, then the right subtree, then the node itself.

Example 1

Input: TreeNode(10, TreeNode(5), TreeNode(15))

Output: [5, 15, 10]

Explanation: Left subtree (5), right subtree (15), root (10).

Example 2

Input: TreeNode(7, None, TreeNode(9, TreeNode(8), None))

Output: [8, 9, 7]

Explanation: Left subtree is None, right subtree (8, then 9), root (7).

Example 3

Input: None

Output: []

Explanation: Empty tree returns empty list.

Example 4

Input: TreeNode(3)

Output: [3]

Explanation: Single node tree returns its value.

Constraints

  • 0 <= number of nodes <= 100
  • -100 <= node value <= 100
Python (current runtime)

Case 1

Input: TreeNode(4, TreeNode(2, None, TreeNode(3)), TreeNode(6))

Expected: [3, 2, 6, 4]

Case 2

Input: TreeNode(1, TreeNode(2, TreeNode(3), None), None)

Expected: [3, 2, 1]

Case 3

Input: TreeNode(5, TreeNode(1), TreeNode(7, TreeNode(6), TreeNode(8)))

Expected: [1, 6, 8, 7, 5]

Case 4

Input: TreeNode(20, None, None)

Expected: [20]