Tree Traversals refer to search operations that visit each node in a binary tree exactly once. Depth-First Search (DFS) traversals include Preorder (Root-L-R), Inorder (L-Root-R), and Postorder (L-R-Root).
Visit root node 4 first, recurse to left subtree [2, 1, 3], then right subtree [6, 5, 7].
Output Sequence: [4, 2, 1, 3, 6, 5, 7]Recurse to leftmost leaf 1, visit parent 2, visit 3, visit root 4, then right subtree [5, 6, 7].
Output Sequence: [1, 2, 3, 4, 5, 6, 7]Recurse to leaves [1, 3], visit parent 2, visit leaves [5, 7], visit parent 6, visit root 4 last.
Output Sequence: [1, 3, 2, 5, 7, 6, 4]Imagine visiting houses in a cul-de-sac. Preorder announces the visit at the entrance. Inorder processes the house after checking the left lane. Postorder announces the visit when exiting the street.
~9 steps (Logarithmic)
O(log N) Tree Height Time
O(N) Tree Nodes Space
Memory growth rate as N expands.
Relative efficiency rating for large N.
| Operation | Time Complexity | Space Complexity |
|---|---|---|
| Inorder Traversal | O(N) | O(H) |
| Preorder Traversal | O(N) | O(H) |
| Postorder Traversal | O(N) | O(H) |
function inorder(root) {
if (!root) return;
inorder(root.left);
console.log(root.val);
inorder(root.right);
}Converts syntax trees into postfix/prefix code formats.
Outputs binary tree contents in sorted order using inorder traversal.
Validate your conceptual understanding, operation mechanics, and core concepts of Tree Traversals.
Remember that Inorder traversal over a Binary Search Tree ALWAYS yields key values in ascending sorted order!
Tree loaded. Choose traversal mode and click Traverse Tree.