PROBLEM DESCRIPTION We are given a tree in which every Node has another attribute called parent which points to its immediate ancestor. We are also given another Node as input, which is present in...
Binary Tree Diameter
PROBLEM DESCRIPTION Return the diameter of a given binary tree. Diameter is defined as the length of its longest path, even if that path doesn’t pass through the node of the tree. SOLUTION This ...
Bubble Sort
Problem Description Implement Bubble Sort (in-place) Solution We can loop through the array and swap the elements if the right one is lesser than the one towards it’s left. We can further optimi...
Power Set
PROBLEM DESCRIPTION For a given list of numbers, return the list of its power set. SOLUTION APPROACH 1 This can be solved recursively. We call the function twice, once assuming that the current...
Invert Binary Tree
PROBLEM DESCRIPTION Invert the binary tree by swapping every every left node in the tree with its right node. SOLUTION import java.util.*; class Program { public static void invertBinaryTree(...
Min Height BST
PROBLEM DESCRIPTION Given a list of integers in sorted order, return the root node of a BST such that the BST has the minimum possible height. SOLUTION Since the list of already sorted, we can i...
Validate BST
PROBLEM DESCRIPTION Given a tree, check whether it’s a BST. SOLUTION APPROACH 1 MaxOf(Left Sub Tree) < Node.value <= MinOf(Right Sub Tree) Using this relation, for each Node, we can ...
Find kth Largest Value in BST
PROBLEM DESCRIPTION Given a BST, find the kth largest element in that BST. SOLUTION The trivial way is to do a inorder traversal of the BST - which will be sorted. Then return the (n-k)th elemen...
Palindrome LinkedList
PROBLEM DESCRIPTION Given the head of a singly linked list, return true if it is a palindrome. Palindrome LinkedList SOLUTION Using Stack (O(n) space complexity) class Solution { public ...
Reverse a LinkedList
PROBLEM DESCRIPTION Reverse a given LinkedList. Reverse a LinkedList SOLUTION ITERATIVE APPROACH class Solution { public ListNode reverseList(ListNode head) { if(head ==...