15% off all plans & roadmaps Use code
See plansProblem editorial
A binary search tree (BST) is a binary tree in which every value in a node's left subtree is smaller than the node's value, and every value in its right subtree is larger. Each node may have zero, one, or two children. The root parameter is the root of the existing BST, represented in the input as a level-order array; null denotes a missing child.
Insert into a Binary Search Tree is a Tree Traversal Patterns (DFS & BFS) problem, in the Recursive Preorder Traversal subpattern. The pattern covers preorder, inorder, level order traversal, LCA, and serialization
The reference solution runs in O(h), where h is the height of the tree. time and O(h) for the recursion stack. space.
The full editorial is part of Pro
It explains the core insight, the complexity analysis and the common mistakes, with a worked solution.
Insert into a Binary Search Tree is a Tree Traversal Patterns (DFS & BFS) problem, in the Recursive Preorder Traversal subpattern. Once the approach above makes sense, write it yourself and then work through the neighbouring problems that reuse it.