Identify the deepest node that breaks the balance factor rule, rotate, and then move up and rotate the next (through recursion)
Insertion has at most 1 rotation
Deletion can have at most h rotations
After, height of all nodes in search path may change (should store height values in nodes so height count is O(1) instead of O(n))
Search
TreeNode* insert(TreeNode* root, int key) { // Insert if (root == nullptr) return new TreeNode(key); else if (key < root->val) root->left = insert(root->left, key); else root->right = insert(root->right, key); // Update height // Rebalance // (pseudo below) return root;}
IF tree is right heavy IF tree's right subtree is left heavy Perform Right Left rotation ELSE Perform Left rotationELSE IF tree is left heavy IF tree's left subtree is right heavy Perform Left Right rotation ELSE Perform Right rotation