Depth of BST Given Insertion Order

medium tree bst ordered set binary search

Problem

You are given a permutation of distinct values. Insert them one by one into an initially empty binary search tree (BST) in the given order: each new value walks down from the root, going left when it is smaller and right when it is larger, until it lands on an empty spot. Return the maximum depth the tree reaches (the root is at depth 1).

Inputorder = [4, 2, 3, 1, 5, 6]
Output3
4 is the root (depth 1). 2 and 5 attach at depth 2. 3, 1 and 6 attach at depth 3. The deepest node sits at depth 3.

class Node:
    def __init__(self, val):
        self.val = val
        self.left = self.right = None

def max_depth_bst(order):
    root = None
    best = 0
    for x in order:
        if root is None:
            root = Node(x)
            best = max(best, 1)
            continue
        node, depth = root, 1
        while True:
            depth += 1
            if x < node.val:
                if node.left is None:
                    node.left = Node(x)
                    break
                node = node.left
            else:
                if node.right is None:
                    node.right = Node(x)
                    break
                node = node.right
        best = max(best, depth)
    return best
function maxDepthBST(order) {
  let root = null, best = 0;
  for (const x of order) {
    if (root === null) {
      root = { val: x, left: null, right: null };
      best = Math.max(best, 1);
      continue;
    }
    let node = root, depth = 1;
    while (true) {
      depth++;
      if (x < node.val) {
        if (node.left === null) { node.left = { val: x, left: null, right: null }; break; }
        node = node.left;
      } else {
        if (node.right === null) { node.right = { val: x, left: null, right: null }; break; }
        node = node.right;
      }
    }
    best = Math.max(best, depth);
  }
  return best;
}
class Solution {
    static class Node { int val; Node left, right; Node(int v) { val = v; } }
    public int maxDepthBST(int[] order) {
        Node root = null;
        int best = 0;
        for (int x : order) {
            if (root == null) {
                root = new Node(x);
                best = Math.max(best, 1);
                continue;
            }
            Node node = root;
            int depth = 1;
            while (true) {
                depth++;
                if (x < node.val) {
                    if (node.left == null) { node.left = new Node(x); break; }
                    node = node.left;
                } else {
                    if (node.right == null) { node.right = new Node(x); break; }
                    node = node.right;
                }
            }
            best = Math.max(best, depth);
        }
        return best;
    }
}
struct Node { int val; Node *left = nullptr, *right = nullptr; Node(int v) : val(v) {} };

int maxDepthBST(vector<int>& order) {
    Node* root = nullptr;
    int best = 0;
    for (int x : order) {
        if (!root) {
            root = new Node(x);
            best = max(best, 1);
            continue;
        }
        Node* node = root;
        int depth = 1;
        while (true) {
            depth++;
            if (x < node->val) {
                if (!node->left) { node->left = new Node(x); break; }
                node = node->left;
            } else {
                if (!node->right) { node->right = new Node(x); break; }
                node = node->right;
            }
        }
        best = max(best, depth);
    }
    return best;
}
Time: O(n · h) (worst case O(n²)) Space: O(n)