Balance a Binary Search Tree Problem

Problem Given the root of a binary search tree, return a balanced binary search tree with the same node values. If there is more than one answer, return any of them. A binary search tree is balanced if the depth of the two subtrees of every node never differs by more than 1. Examples Example 1: 1 \ 2 \ 3 \ 4 ...

Given a binary search tree with 2 nodes swapped find number of pairs not following BST properties

Given a binary search tree with 2 nodes swapped find number of pairs not following BST properties Problem Given a binary search tree with 2 nodes swapped find number of pairs not following BST properties Follow up: Recover Binary Search Tree Problem Examples Example 1 Consider the BST: 10 / \ 5 (8) / \ 2 (20) ...

Validate Binary Search Tree Problem

Problem Given the root of a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defined as follows: The left subtree of a node contains only nodes with keys less than the node’s key. The right subtree of a node contains only nodes with keys greater than the node’s key. Both the left and right subtrees must also be binary search trees. Examples Example 1: ...

Largest Binary Search Tree BST Subtree

Problem Given a binary tree, find the largest subtree which is a Binary Search Tree (BST), where largest means subtree with largest number of nodes in it. Note: A subtree must include all of its descendants. Examples Example 1: 10 / \ [5] 15 / \ \ [1] [8] 7 ...

Inorder Successor in Binary Search Tree

Problem Given a Binary Search tree, find the inorder successor of a node. Definition See the definition here. Examples Example 1: 10 / \ 5 30 / \ 22 35 ...

Kth largest element in Binary Search Tree

Problem Given a Binary Search Tree (BST) and a positive integer k, find the k’th largest element in the Binary Search Tree. Examples Example 1: Input: 2 / \ 1 3 and k = 2 Return : 2 As 2 is the second smallest element in the tree. ...

Convert Sorted Array to height-balanced Binary Search Tree

Problem Given an integer array nums where the elements are sorted in ascending order, convert it to a height-balanced binary search tree. Examples Example 1: Input: nums = [1, 2, 3, 4, 5, 6] Output: [3,2,5,1,null,4,6] Explanation: [4,2,5,1,3,null,6] is also accepted. ...

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 ...

Kth Smallest Element in a BST Problem

Problem Given the root of a binary search tree, and an integer k, return the kth smallest value (1-indexed) of all the values of the nodes in the tree. Examples Example 1: 3 / \ / \ 1 4 \ / \ / 2 ...

Path Sum 2 - find all root to leaf paths

Problem Given a binary tree and a sum, find all root-to-leaf paths where each path’s sum equals the given sum. Examples Example 1: graph TD; A[5] --> B[4] & C[8] B --> D[11] & E[null] C --> F[13] & G[4] D --> H[7] & I[2] G --> L[5] & M[1] style A fill:#f9f style B fill:#f9f style C fill:#f9f style D fill:#f9f style I fill:#f9f style G fill:#f9f style L fill:#f9f ...

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