-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
65b8599
commit e396f0e
Showing
16 changed files
with
398 additions
and
58 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 46 additions & 0 deletions
46
src/com/problems/binarysearchtree/MinimumDistanceBetweenBSTNodes.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
42
src/com/problems/binarytree/EvaluateBooleanBinaryTree.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |
Oops, something went wrong.