Return the postorder traversal of a binary tree: visit left subtree, then right subtree, then the node itself.
Problem
Given the root of a binary tree, return the values of its nodes in postorder traversal.
In postorder traversal, you process each node in the following order:
- Traverse the left subtree
- Traverse the right subtree
- Visit the current node
Your task is to output the node values in that order.
Notes
- The tree may be empty.
- Each node value should appear exactly once in the result if the node exists.
Input Format
- A binary tree root node is provided as input.
- The tree is represented using the platform's standard binary tree encoding.
Output Format
- Return an array of integers containing the postorder traversal of the tree.
Constraints
- The number of nodes is non-negative.
- Node values can be any integers allowed by the platform.
- Your solution should run in time for nodes.
Example 1
Input
root = [1,null,2,3]
Output
[3,2,1]
Explanation
Left subtree of 1 is empty. Visit the right subtree rooted at 2, which has left child 3. Postorder is 3, 2, 1.
Example 2
Input
root = [1,2,3,4,5,null,6]
Output
[4,5,2,6,3,1]
Explanation
Traverse left subtree of 1: postorder is 4, 5, 2. Traverse right subtree: postorder is 6, 3. Then visit 1.
Premium problem context
Unlock deeper context for this problem
Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.