Binary Tree Postorder Traversal

EasyTreesdfs

Solve this the way you would in the interview: out loud, on a timer, with an AI interviewer asking follow-ups while you code. You get a scored report at the end.

Practice this as a mock interviewFree to start, no account needed

Problem

Given the root of a binary tree, return the list of its node values in **postorder**: for every node, all values from its left subtree come first, then all values from its right subtree, then the node's own value. Return an empty list for an empty tree. The recursive version is short; as a follow-up, produce the same order iteratively with an explicit stack.

Examples

Input: root = [1,null,2,3]
Output: [3,2,1]
Node 2 is the right child of 1 and 3 is the left child of 2.
Input: root = []
Output: []
No nodes, nothing to visit.
Input: root = [1,2,3,4,5,null,8,null,null,6,7,9]
Output: [4,6,7,5,2,9,8,3,1]
The left subtree of 1 yields 4,6,7,5,2, the right subtree yields 9,8,3, and 1 comes last.

Constraints

  • 0 <= number of nodes <= 1000
  • -100 <= Node.val <= 100

What the interviewer grades

A correct solution is one of five scores. The report also grades problem understanding (did you clarify inputs and edge cases before coding), problem solving, communication while you work, code quality, and testing. See a sample report, or read up on the pattern in the lessons.

More dfs problems