BinarySearchTree

ds/binary-tree~ BinarySearchTree

AVL Tree

Constructor

new BinarySearchTree()

Source:

Methods

(static) lowestCommonAncestor(root, p, q) → {number}

Given a binary search tree (BST), find the lowest common ancestor (LCA) node of two given nodes in the BST

For example,
Input: root = [6, 2, 8, 0, 4, 7, 9, null, null, 3, 5], p = 2, q = 8
Output: 6

Source:
See:
Parameters:
Name Type Description
root TreeNode

root of binary search tree

p TreeNode

First tree node

q TreeNode

Second tree node

Returns:
Type:
number

Lowest common ancestor between two nodes

insert(value, current)

Insert the node into the binary tree at given node. If not provided, root will be consider as current.

Time Complexity: O(logN) in the average case and O(N) in worst case.

Source:
Parameters:
Name Type Default Description
value T

value that needs to be inserted

current TreeNode null

Current Node

Returns:

void