Binary Tree

A tree where each node has at most 2 children (left, right). Binary Search Trees provide O(log n) search when balanced.

Syntax

dsa
class TreeNode { val; left; right; }

Example

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

// BST property: left < node < right
// In-order traversal visits nodes in sorted order
// Height balanced trees: AVL, Red-Black -> O(log n)