Lowest Common Ancestor in a Binary Search Tree

Problem Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BST. Definiton Lowest Common Ancestor Definition Problems related to it - Lowest Common Ancestor Definition Examples Example 1: graph TD; 6; 6 --- 2; 6 --- 8; 2 --- 0; 2 --- 4; 8 --- 7; 8 --- 9; 4 --- 3; 4 --- 5; style 2 fill:#FF9933 style 8 fill:#FF9933 style 6 fill:#3399FF ...

Diameter Of a Binary Tree

Problem Given a binary tree, find the diameter of it. Definition Diameter of tree is defined as a longest path or route between any two nodes in a tree. The path may or may not for through the root. Example Example 1: 1 / \ 2 3 / \ 4 5 Input: root = [1,2,3,4,5] Output: 3 Explanation: 3 is the length of the path [4,2,1,3] or [5,2,1,3]. ...

Linked List Cycle 1 - Detect Cycle

Problem Given head, the head of a linked list, determine if the linked list has a cycle in it. Return true if there is a cycle in the linked list. Otherwise, return false. Examples Example 1 Input: head = [0, 1, 3, 2, 4] output: true Explanation: Cycle starts at node 2 ...

Maximum Depth of Binary Tree

Maximum Depth of Binary Tree Problem Given a binary tree, find its maximum depth. Definition A binary tree’s maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node. The number of nodes along the longest path from the root node down to the farthest leaf node. Depth of root node is 0. Example Example 1 ...

Same Tree Problem

Problem Given the roots of two binary trees p and q, write a function to check if they are the same or not. Two binary trees are considered the same if they are structurally identical, and the nodes have the same value. Example Input: Two binary Search Trees ...

Binary Tree Preorder Traversal

Problem Given the root of a binary tree, return the preorder traversal of its nodes’ values. Examples Example 1: 1 \ 2 / 3 Input: root = [1,null,2,3] Output: [1,2,3] ...

Find Second largest element in an array

Problem Given an array of integers, our task is to write a program that efficiently finds the second largest element present in the array. Similar problem - Find smallest and second smallest elements in an array Examples Example 1: Input : arr[] = {12, 35, 1, 10, 34, 1} Output : 34 Example 2: Input : arr[] = {10, 5, 10} Output : 5 ...

Find Deepest Node in a Binary Tree

Problem Given a binary tree, find the deepest node in it. If there are multiple, return the right most node. (Good to check with the interviewer - some may say left most). Examples Example 1: 1 / \ 2 3 / \ / \ 4 5 6 7 ...

Merge Two Sorted Linked Lists

Problem Given two sorted Linked Lists, how to merge them into the third list in sorted order? Example Example 1: Input: list1 = [1,2,4], list2 = [1,3,4] Output: [1,1,2,3,4,4] Merged List: 1 -> 1 -> 2 -> 3 -> 4 -> 4 ...

Is Subsequence Problem

Problem Given two strings s and t, return true if s is a subsequence of t, or false otherwise. OR Given two strings str1 and str2, find if str1 is a subsequence of str2. Subarrays vs Subsequences vs Subsets Definition Follow up Expected time complexity is linear. Suppose there are lots of incoming s, say s1, s2, ..., sk where k >= 109, and you want to check one by one to see if t has its subsequence. In this scenario, how would you change your code? Examples Example 1: ...

This site uses cookies to improve your experience on our website. By using and continuing to navigate this website, you accept this. Privacy Policy