An interactive step-by-step visualisation of iterative inorder tree traversal. Watch how an explicit stack replaces recursion to visit nodes in Left-Root-Right order, producing sorted output for a BST.
1def inorder_traversal(root):2 result = []3 stack = []4 curr = root56 while curr or stack:7 # Go left as far as possible8 while curr:9 stack.append(curr)10 curr = curr.left1112 # Pop and process13 curr = stack.pop()14 result.append(curr.val)1516 # Move to right subtree17 curr = curr.right1819 return result
We have a binary search tree. Our goal is to visit all nodes in inorder (Left-Root-Right), which for a BST gives us sorted output.