Problem Statement: You are given BST root and a key, find the inorder predecessor and successor for that key. If the key is not found, then you need to return the 2 values in which the key lies. Solution If the root is NULL then …
Problem Statement: You are given a BST root node, you need to find the max and min value. Solution To get the min value, traverse the node from root to left recursively until left node is NULL. To get the max value, traverse the node from …
Problem Statement: You are given BST root node and a key value. You need to delete that node from BST. Solution There are few cases to be considered. 1. If the key is equal to the leaf node, then we can delete directly. 2. If the …
Problem Statement: Given a BST and a key element, you need to find if the key element is present in BST or not. Solution The solution is very simple. We take the key element and compare with the root, if the key element is smaller then …
Problem Statement: You are given a binary tree, you need to convert into DLL. Example In-place convert Binary Tee to a Doubly Linked List 4 <-> 5 <-> 14 <-> 10 <-> 15 <-> 20 <-> 30 Solution In this solution we use in-order …
Problem Statement: You are given an array representation of a complete binary tree. You need to find the minimum swaps required to convert into BST. Example Consider the array: {1, 2, 3} Binary tree will be: 1 / \ 2 3 After swapping node 1 …
Problem Statement: You are given 2 arrays with inorder and pre-order. You need to construct a binary tree with the given array. Example Input: preorder = , inorder = Output: Solution We know that in pre-order traversal starts from root, goes to the left …