Binary Search Trees

Inorder Successor in Binary Search Tree

Problem Statement Given a binary search tree and a node in it, find the in-order successor of that node in the BST. Sample Test Case Problem Solution A node’s inorder successor is node with least value in its right subtree i.e. its right subtree’s left most child. If right subtree of the node doesn’t exists, …

Inorder Successor in Binary Search Tree Read More »

Binary Search Tree to Greater Sum Tree

Problem Statement Given the root of a binary search tree with distinct values, modify it so that every node has a new value equal to the sum of the values of the original tree that are greater than or equal to node.val. As a reminder, a binary search tree is a tree that satisfies these constraints: The left subtree of a node …

Binary Search Tree to Greater Sum Tree Read More »

Convert Sorted Array To Binary Search Tree

Problem Statement Given an array where elements are sorted in ascending order, convert it to a height balanced BST. For this problem, a height-balanced binary tree is defined as a binary tree in which the depth of the two subtrees of every node never differ by more than 1. Sample Test Case Problem Solution Since …

Convert Sorted Array To Binary Search Tree Read More »

Range Sum Of BST

Range Sum of BST Link Given the root node of a binary search tree, return the sum of values of all nodes with value between L and R (inclusive). The binary search tree is guaranteed to have unique values. Example 1: Input: root = [10,5,15,3,7,null,18], L = 7, R = 15 Output: 32 Example 2: …

Range Sum Of BST Read More »

Convert BST To Greater Tree

Convert BST to Greater Tree Link Given a Binary Search Tree (BST), convert it to a Greater Tree such that every key of the original BST is changed to the original key plus sum of all keys greater than the original key in BST. Example: Input: The root of a Binary Search Tree like this: …

Convert BST To Greater Tree Read More »

Closest Binary Search Tree Value

Closest Binary Search Tree Value Link Given a non-empty binary search tree and a target value, find the value in the BST that is closest to the target. Note: Given target value is a floating point. You are guaranteed to have only one unique value in the BST that is closest to the target. Example: …

Closest Binary Search Tree Value Read More »

Lowest Common Ancestor of a Binary Search Tree

Lowest Common Ancestor of a Binary Search Tree Link Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BST. According to the definition of LCA on Wikipedia: “The lowest common ancestor is defined between two nodes p and q as the lowest node in T that …

Lowest Common Ancestor of a Binary Search Tree Read More »

Scroll to Top
[gravityforms id="5" description="false" titla="false" ajax="true"]