Set your Publisher ID in src/config/ads.ts to display live ads.
Binary Search Trees (BST) & Tree Traversals
In-order, Pre-order, Post-order, Level-order BFS, and tree balance
A Binary Search Tree (BST) is a hierarchical node structure where every node has at most two children, satisfying the property: all left descendants < root < all right descendants.
The 4 Core Tree Traversals
In-order (Left, Root, Right): Visits nodes in strictly ascending numerical order on a BST.
Pre-order (Root, Left, Right): Useful for serializing / copying tree structures.
Post-order (Left, Right, Root): Useful for deleting trees or calculating directory sizes bottom-up.
Breadth-First Search (Level-Order): Visits nodes level by level using a queue.
class TreeNode {
constructor(val, left = null, right = null) {
this.val = val;
this.left = left;
this.right = right;
}
}
function inOrderTraversal(root, result = []) {
if (!root) return result;
inOrderTraversal(root.left, result); // Left
result.push(root.val); // Root
inOrderTraversal(root.right, result); // Right
return result;
}
// Tree: 4
// / \
// 2 6
const bst = new TreeNode(4, new TreeNode(2), new TreeNode(6));
console.log("Sorted output:", inOrderTraversal(bst)); // [2, 4, 6]Set your Publisher ID in src/config/ads.ts to display live ads.
Test Yourself With Exercise
Which tree traversal order on a Binary Search Tree produces elements in sorted ascending order?