Playground

Inorder Traversal

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

Inorder Traversal

Practice inorder traversal (Left → Root → Right) 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%

INORDER

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

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 inorder(node) {
  if (!node) return [];
  return [...inorder(node.left), node.val, ...inorder(node.right)];
}

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