Problem Statement: You are given the root node of BST and 2 key elements. You need to find the distance between the 2 nodes. Solution So as the tree is a BST, if both the keys are greater than the current node, then move right else …
Problem Statement: You are given a root of BST and 2 values. You need to return the sum of the nodes that falls within the range. Solution For solution we follow below steps: 1. We take a node, and check if the node value is less …
Problem Statement: You are given BST and a kth element in the given tree. Then you need to give the kth largest element. Solution Here we need to do reverse inorder traversal of BST and we need to keep a count of nodes visited. It means, …
Problem Statement: You are given two balanced BST, you need to merge the two given balanced BST. Solution The solution is very simple. WKT in-order traversal of BST will give the sorted order. So we need to perform in-order traversal of both BST, we will get …
Problem Statement: You are given a BST root node, you need to make it as a balanced BST. Example Input: 3 / 2 / 1 Output: 2 / \ 1 3 Solution The solution is simple. We follow below steps: 1. Get the nodes of …
Problem Statement: You are given an array representing a preorder traversal of a Binary Search Tree and construct the BST. Solution WKT the first element in a pre-order traversal is always the root. So we take the first element as root, then we search for the …
Problem Statement: You are given a BST root node and 2 node values n1 and n2, you need to find the Lowest Common Ancestor. Solution As we know the property of BST, check if the current node is less than both n1 and n2 then LCA …