Playground

Postorder Traversal

Practice postorder traversal (Left-Right-Root) with step-by-step state, history, and traversal logs.

Postorder Traversal

Practice postorder traversal (Left → Right → Root) with visual state, execution logs, and recursive trace output.

class TreeNode {
data: any;
left: TreeNode | null;
right: TreeNode | null;
}
Add
Parent
Delete
Add parent: none | Delete target: none. Delete removes only the selected node and reconnects children.
Traverse
LLRRR1ROOT24536
100%

POSTORDER

function postorder(node):
if node == null: return
postorder(node.left)
postorder(node.right)
VISIT node

Console Output

NODES: 6
>Waiting for operations...

Want to see how the recursive code works?

Copy this code and paste it in the Recursion Tracer to visualize the call stack step-by-step.

OPEN TRACER
class TreeNode {
  constructor(val) {
    this.val = val;
    this.left = null;
    this.right = null;
  }
}

function buildTree(arr) {
  if (!arr.length || arr[0] === null) return null;
  const root = new TreeNode(arr[0]);
  const queue = [root];
  let i = 1;
  while (queue.length > 0 && i < arr.length) {
    const node = queue.shift();
    if (i < arr.length && arr[i] !== null) {
      node.left = new TreeNode(arr[i]);
      queue.push(node.left);
    }
    i++;
    if (i < arr.length && arr[i] !== null) {
      node.right = new TreeNode(arr[i]);
      queue.push(node.right);
    }
    i++;
  }
  return root;
}

function postorder(node) {
  if (!node) return [];
  return [...postorder(node.left), ...postorder(node.right), node.val];
}

// Build and traverse
const input = [1, 2, 3, 4, 5, null, 6];
const root = buildTree(input);
console.log("Postorder:", postorder(root));