Skip to content

Commit

Permalink
commit
Browse files Browse the repository at this point in the history
  • Loading branch information
abhishekghoshh committed Jan 6, 2025
1 parent 65b8599 commit e396f0e
Show file tree
Hide file tree
Showing 16 changed files with 398 additions and 58 deletions.
3 changes: 2 additions & 1 deletion resources/binary-search-tree.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,13 @@
### General
- [Introduction to Binary Search Trees](/src/com/problems/binarysearchtree/IntroductionToBinarySearchTree.java)
- [Search in a Binary Search Tree](/src/com/problems/binarysearchtree/SearchInBinarySearchTree.java)
- [Minimum Distance Between BST Nodes](/src/com/problems/binarysearchtree/MinimumDistanceBetweenBSTNodes.java)
- [Minimum element in BST](/src/com/problems/binarysearchtree/MinimumElementInBinarySearchTree.java)
- [Maximum element in BST](/src/com/problems/binarysearchtree/MaximumElementInBinarySearchTree.java)
- [Ceil in a Binary Search Tree](/src/com/problems/binarysearchtree/CeilInBinarySearchTree.java)
- [Floor in a Binary Search Tree](/src/com/problems/binarysearchtree/FloorInBinarySearchTree.java)
- [Convert Sorted Array to Binary Search Tree](/src/com/problems/binarysearchtree/ConvertSortedArrayToBinarySearchTree.java)
- [Insert a Given Node in Binary Search Tree](/src/com/problems/binarysearchtree/InsertNodeInBinarySearchTree.java)
- [Insert into a Binary Search Tree](/src/com/problems/binarysearchtree/InsertIntoABinarySearchTree.java)
- [Delete a Node in Binary Search Tree](/src/com/problems/binarysearchtree/DeleteNodeInBinarySearchTree.java)
- [Kth largest/smallest element in Binary Search Tree](/src/com/problems/binarysearchtree/KthSmallestLargestElementInBinaryTree.java)
- [Validate Binary Search Tree](/src/com/problems/binarysearchtree/CheckIfTreeIsBinarySearchTree.java)
Expand Down
8 changes: 7 additions & 1 deletion resources/binary-tree.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,18 @@
- [Second minimum value in the binary tree](/src/com/problems/binarytree/SecondMinimumValueInBinaryTree.java)
- [Check if the Binary Tree is Balanced Binary Tree](/src/com/problems/binarytree/CheckIfTheBinaryTreeIsHeightBalanced.java)
- [Diameter of a Binary Tree](/src/com/problems/binarytree/DiameterOfBinaryTree.java)
- [Path Sum](/src/com/problems/binarytree/PathSum.java)
- [Path Sum II](/src/com/problems/binarytree/PathSum2.java)
- [Maximum Path Sum in Binary Tree from any node to any node](/src/com/problems/binarytree/MaximumPathSumBinaryTreeFromAnyNodeToAnyNode.java)
- [Maximum Path Sum in Binary Tree from leaf to leaf](/src/com/problems/binarytree/MaximumPathSumNodesBinaryTreeFromLeafToLeafNode.java)
- [Check if two trees are identical](/src/com/problems/binarytree/CheckTwoTreesAreIdenticalOrNot.java)
- [Invert/Flip Binary Tree (Create)](/src/com/problems/binarytree/InvertBinaryTree.java)
- [Subtree of Another Tree](/src/com/problems/binarytree/SubtreeOfAnotherTree.java)
- [Merge Two Binary Trees](/src/com/problems/binarytree/MergeTwoBinaryTrees.java)
- [Range Sum of BST](/src/com/problems/binarytree/RangeSumOfBST.java)
- [Leaf-Similar Trees](/src/com/problems/binarytree/LeafSimilarTrees.java)
- [Evaluate Boolean Binary Tree](/src/com/problems/binarytree/EvaluateBooleanBinaryTree.java)



- [Top view of a Binary Tree](/src/com/problems/binarytree/TopViewOfBinaryTree.java)
Expand All @@ -45,7 +51,7 @@
- [Right view of Binary Tree](/src/com/problems/binarytree/RightViewOfBinaryTree.java)

- [Boundary Traversal of Binary Tree](/src/com/problems/binarytree/BoundaryTraversalInBinaryTree.java)
- [Symmetric Binary Tree](/src/com/problems/binarytree/CheckForSymmetricalBinaryTree.java)
- [Symmetric Tree](/src/com/problems/binarytree/SymmetricTree.java)
- [Print Root to Node Path in a Binary Tree](/src/com/problems/binarytree/PrintRootToNodePath.java)
- [Lowest Common Ancestor of a Binary Tree](/src/com/problems/binarytree/LowestCommonAncestorOfBinaryTree.java)
- [Maximum Width of Binary Tree](/src/com/problems/binarytree/MaximumWidthOfBinaryTree.java)
Expand Down
21 changes: 18 additions & 3 deletions src/com/ds/binarytree/TNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,21 @@
public class TNode {
public static final int NULL = Integer.MIN_VALUE;
public int data;
public int val;
public TNode left, right, next;

public TNode(int data) {
this.data = data;
this.data = this.val = data;
}

public TNode(int val, TNode left, TNode right) {
this.data = val;
this.data = this.val = val;
this.left = left;
this.right = right;
}

public TNode data(int data) {
this.data = data;
this.data = this.val = data;
return this;
}

Expand Down Expand Up @@ -66,6 +67,20 @@ public static TNode withNodes(int... nums) {
}
return nodes[0];
}
public static TNode withObjectNodes(Integer... nums) {
int n = nums.length;
if (n == 0) return null;
TNode[] nodes = new TNode[n];
for (int i = 0; i < n; i++)
nodes[i] = (nums[i] != null) ? new TNode(nums[i]) : null;
for (int i = 0; i < n; i++) {
if (null == nodes[i]) continue;
int left = i * 2 + 1, right = i * 2 + 2;
nodes[i].left = left < n ? nodes[left] : null;
nodes[i].right = right < n ? nodes[right] : null;
}
return nodes[0];
}

public static TNode imbalancedBST(int n) {
int[] nums = new int[n];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,11 @@
/*
* Problem link :
* https://leetcode.com/problems/convert-sorted-array-to-binary-search-tree/description/
*
* Solution link :
* https://www.youtube.com/watch?v=0K0uCMYq5ng
*
* https://neetcode.io/solutions/convert-sorted-array-to-binary-search-tree
*/
public class ConvertSortedArrayToBinarySearchTree {
public static void main(String[] args) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,56 +5,55 @@

/*
* Problem link :
* https://leetcode.com/problems/insert-into-a-binary-search-tree/
* https://practice.geeksforgeeks.org/problems/insert-a-node-in-a-bst/1
* https://www.codingninjas.com/studio/problems/insert-into-a-binary-search-tree_1279913
* https://leetcode.com/problems/insert-into-a-binary-search-tree/description/
* https://www.geeksforgeeks.org/problems/insert-a-node-in-a-bst/1
* https://www.naukri.com/code360/problems/insert-into-a-binary-search-tree_1279913
*
* Solution link :
* https://www.youtube.com/watch?v=FiFiNvM29ps&list=PLgUwDviBIf0q8Hkd7bK2Bpryj2xVJk8Vk&index=44
* https://www.youtube.com/watch?v=Cpg8f79luEA
*
* https://takeuforward.org/binary-search-tree/insert-a-given-node-in-binary-search-tree/
* https://neetcode.io/solutions/insert-into-a-binary-search-tree
*
*/
public class InsertNodeInBinarySearchTree {
public class InsertIntoABinarySearchTree {

public static void main(String[] args) {
type1();
type2();
}

// iterative way of adding nodes to the leaf
// as this is an iterative approach we need to have a track of the parent node as well
private static void type2() {
TNode root = TNode.withNodes(4, 2, 7, 1, 3);
int val = 5;
root = insertIntoBST2(root, val);
PrintUtl.levelOrder(root);
}

private static TNode insertIntoBST2(TNode root, int target) {
if (root == null) return new TNode(target);
TNode node = root;
private static TNode insertIntoBST2(TNode root, int val) {
if (root == null) return new TNode(val);
// iteratively going to the specific node and alsa updating the parent node
TNode parent = root, node = root;
while (null != node) {
// we will go to the left if the target is lesser than the node
if (target < node.data) {
// if the left is null, then we will successfully place the node there
if (node.left == null) {
node.left = new TNode(target);
break;
}
parent = node;
if (val < node.val) {
node = node.left;
} else {
// we will go to the right if the target is greater than the node
// if the right is null, then we will successfully place the node there
if (node.right == null) {
node.right = new TNode(target);
break;
}
node = node.right;
}
}
if (val < parent.val) parent.left = new TNode(val);
else parent.right = new TNode(val);
return root;
}


// recursive way of adding nodes to the leaf
// recursively traverse to the specific position
// each time it will either go to the left side if target < root-val
// or go to the right side if root-val < target
private static void type1() {
TNode root = TNode.withNodes(4, 2, 7, 1, 3);
PrintUtl.levelOrder(root);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,25 @@

/*
* Problem link :
* https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-search-tree/
* https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-search-tree/description/
* https://neetcode.io/problems/lowest-common-ancestor-in-binary-search-tree
* https://www.geeksforgeeks.org/problems/lowest-common-ancestor-in-a-bst/1
*
* Solution link :
* https://www.youtube.com/watch?v=cX_kPV_foZc&list=PLgUwDviBIf0q8Hkd7bK2Bpryj2xVJk8Vk&index=48
* https://www.youtube.com/watch?v=gs2LMfuOR9k
*
* https://takeuforward.org/binary-search-tree/lca-in-binary-search-tree/
* https://neetcode.io/solutions/lowest-common-ancestor-of-a-binary-search-tree
*
*/
public class LowestCommonAncestorInBinarySearchTree {

// todo Lowest Common Ancestor of a Binary Search Tree is quite easier than Lowest Common Ancestor of a Binary Tree
// in binary search tree we have left < root < right
// so if p and q both less than root so the common ancestor will be in the left
// if p and q both greater than root so common ancestor will be on the right
// the last condition is p and q are in opposite side, then the current node is then common ancestor
public static void main(String[] args) {
type1();
type2();
Expand All @@ -25,24 +34,23 @@ public static void main(String[] args) {
// we will go left if root data is greater than both p and q.
// we will go right if both p and q are bigger
// while traversing anywhere if we find that p and q are in opposite side
// that node will be the common ancestor
// recursive ways
// that node will be the common ancestor recursive ways
private static void type2() {
TNode root = TNode.makeBST(15);
TNode p = new TNode(7);
TNode q = new TNode(15);
TNode node = lca1(root, p, q);
TNode node = lca2(root, p, q);
System.out.println(node);
}

private static TNode lca1(TNode root, TNode p, TNode q) {
private static TNode lca2(TNode root, TNode p, TNode q) {
if (null == root) return null;
if (root.data > p.data && root.data > q.data)
// node is greater than both of them
return lca1(root.left, p, q);
return lca2(root.left, p, q);
else if (root.data < p.data && root.data < q.data)
// node data is lesser than both of them
return lca1(root.right, p, q);
return lca2(root.right, p, q);
else
// both p and q are in opposite side
return root;
Expand All @@ -58,20 +66,24 @@ private static void type1() {
TNode root = TNode.makeBST(15);
TNode p = new TNode(7);
TNode q = new TNode(15);
TNode node = root;
while (null != node) {
// node is greater than both of them
if (node.data > p.data && node.data > q.data)
node = node.left;
// node data is lesser than both of them
else if (node.data < p.data && node.data < q.data)
node = node.right;
TNode ans = lca1(root, p, q);
System.out.println(ans);
}

private static TNode lca1(TNode root, TNode p, TNode q) {
while (null != root) {
// root is greater than both of them
if (root.data > p.data && root.data > q.data)
root = root.left;
// root data is lesser than both of them
else if (root.data < p.data && root.data < q.data)
root = root.right;
else
// both p and q are in opposite side
break;
return root;

}
System.out.println(node);
return null;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package com.problems.binarysearchtree;

import com.ds.binarytree.TNode;

/*
* problem links :
* https://leetcode.com/problems/minimum-distance-between-bst-nodes/description/
* https://leetcode.com/problems/minimum-absolute-difference-in-bst/description/
*
* Solution link :
* https://www.youtube.com/watch?v=joxx4hTYwcw
*
* https://neetcode.io/solutions/minimum-distance-between-bst-nodes
* */
public class MinimumDistanceBetweenBSTNodes {
public static void main(String[] args) {
type1();
}

// if we do an inorder traversal we will get the sorted list
// and in a sorted list we will get the minimum difference only by checking num[i-1] and nums[i]
// so we will carry 2 variables prev and max
private static void type1() {
TNode root = TNode.withObjectNodes(1, 0, 48, null, null, 12, 49);
int ans = minDiffInBST(root);
System.out.println(ans);
}


static int prev = -1, min = Integer.MAX_VALUE;

public static int minDiffInBST(TNode root) {
traverse(root);
return min;
}

private static void traverse(TNode root) {
if (null == root) return;
traverse(root.left);
if (prev != -1) {
min = Math.min(min, (root.val - prev));
}
prev = root.val;
traverse(root.right);
}
}
42 changes: 42 additions & 0 deletions src/com/problems/binarytree/EvaluateBooleanBinaryTree.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package com.problems.binarytree;

import com.ds.binarytree.TNode;

/*
* problem links :
* https://leetcode.com/problems/evaluate-boolean-binary-tree/description/
*
* Solution link :
* https://www.youtube.com/watch?v=9a_cP54jn8Q
*
* https://neetcode.io/solutions/evaluate-boolean-binary-tree
* */
public class EvaluateBooleanBinaryTree {
public static void main(String[] args) {
type1();
}

private static void type1() {
TNode root = TNode.makeBST(2, 1, 3, TNode.NULL, TNode.NULL, 0, 1);
boolean ans = evaluateTree(root);
System.out.println(ans);
}

public static boolean evaluateTree(TNode root) {
// if node is null then we will return true
if (null == root) return true;
// if it is leaf node then we will check if it is 1 or not
if (root.left == null && root.right == null) {
return (root.val == 1);
}
// we will collect the values from left and right
boolean left = evaluateTree(root.left);
boolean right = evaluateTree(root.right);
// if thw val is 2 then it is OR if the value is 3 then it is AND
if (root.val == 2) {
return left || right;
} else {
return left && right;
}
}
}
Loading

0 comments on commit e396f0e

Please sign in to comment.