Skip to content

Commit

Permalink
problem: new problem solution - 501 . Find Mode in Binary Search Tree
Browse files Browse the repository at this point in the history
  • Loading branch information
squxq committed Nov 5, 2023
1 parent 326a4b1 commit 0fcc8ae
Show file tree
Hide file tree
Showing 3 changed files with 204 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ My approach to solving LeetCode problems typically involves the following steps:
| 700 | [Search in a Binary Search Tree](https://leetcode.com/problems/search-in-a-binary-search-tree/) | Algorithms | [TypeScript](./problems/algorithms/searchInABinarySearchTree/SearchInABinarySearchTree.ts) | Easy |
| 108 | [Convert Sorted Array to Binary Search Tree](https://leetcode.com/problems/convert-sorted-array-to-binary-search-tree/) | Algorithms | [TypeScript](./problems/algorithms/convertSortedArrayToBinarySearchTree/ConvertSortedArrayToBinarySearchTree.ts) | Easy |
| 530 | [Minimum Absolute Difference in BST](https://leetcode.com/problems/minimum-absolute-difference-in-bst/) | Algorithms | [TypeScript](./problems/algorithms/minimumAbsoluteDifferenceInBst/MinimumAbsoluteDifferenceInBst.ts) | Easy |
| 501 | [Find Mode in Binary Search Tree](https://leetcode.com/problems/find-mode-in-binary-search-tree/) | Algorithms | [TypeScript](./problems/algorithms/findModeInBinarySearchTree/FindModeInBinarySearchTree.ts) | Easy |
| ... | ... | ... | ... | ... |

In this table:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
// Source : https://leetcode.com/problems/find-mode-in-binary-search-tree/
// Author : squxq
// Date : 2023-11-05

import { NodeTree, findMode, type BST } from "./FindModeInBinarySearchTree"; // Import the necessary code from your file

describe("findMode", () => {
it("should return an empty array for an empty tree", () => {
const root: BST = null;
const result = findMode(root);
expect(result).toEqual([]);
});

it("should return the correct mode for a single-node tree", () => {
const root: BST = new NodeTree(1);
const result = findMode(root);
expect(result).toEqual([1]);
});

it("should return the correct mode for a tree with multiple modes", () => {
const root: BST = new NodeTree(2, null, new NodeTree(2));
const result = findMode(root);
expect(result).toContain(2); // 2 is a mode
});

it("should handle negative values and zero", () => {
const root: BST = new NodeTree(
-1,
new NodeTree(-2),
new NodeTree(0, new NodeTree(-1), new NodeTree(0)),
);
const result = findMode(root);
expect(result).toEqual([-1, 0]); // -1 and 0 are modes
});

it("should return the correct mode for a tree with large values", () => {
const root: BST = new NodeTree(
100000,
new NodeTree(100000),
new NodeTree(200000),
);
const result = findMode(root);
expect(result).toEqual([100000, 200000]); // 100000 and 200000 are modes
});

it("should handle a tree with duplicates", () => {
const root: BST = new NodeTree(
1,
new NodeTree(1, null, new NodeTree(2)),
new NodeTree(2),
);
const result = findMode(root);
expect(result).toEqual([1, 2]); // 1 and 2 are modes
});

it("should return modes in any order", () => {
const root: BST = new NodeTree(
1,
new NodeTree(2, null, new NodeTree(2)),
new NodeTree(2),
);
const result = findMode(root);
expect(result).toContain(2); // 2 is a mode
expect(result).toContain(1); // 1 is a mode
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
// Source : https://leetcode.com/problems/find-mode-in-binary-search-tree/
// Author : squxq
// Date : 2023-11-05

/*****************************************************************************************************
*
* Given the root of a binary search tree (BST) with duplicates, return all the mode(s) (i.e., the
* most frequently occurred element) in it.
*
* If the tree has more than one mode, return them in any order.
*
* Assume a BST is defined as follows:
*
* The left subtree of a node contains only nodes with keys less than or equal to the node's
* key.
* The right subtree of a node contains only nodes with keys greater than or equal to the
* node's key.
* Both the left and right subtrees must also be binary search trees.
*
* Example 1:
*
* Input: root = [1,null,2,2]
* Output: [2]
*
* Example 2:
*
* Input: root = [0]
* Output: [0]
*
* Constraints:
*
* The number of nodes in the tree is in the range [1, 10^4].
* -10^5 <= Node.val <= 10^5
*
* Follow up: Could you do that without using any extra space? (Assume that the implicit stack space
* incurred due to recursion does not count).
******************************************************************************************************/

// Data Definitions:

/**
* BST (Binary Search Tree) is one of:
* null
* NodeTree
* interp. null means, no BST, or empty BST
*/
export type BST = NodeTree | null;

/**
* NodeTree is: new NodeTree(number, NodeTree | null, NodeTree | null)
* interp. new NodeTree(val, left, right) is a BST node with:
* - val, the node's key
* - left, the node's left subtree
* - right, the node's right subtree
* INVARIANT (for a given node):
* - val is greater (>) than all keys (val) in left child (left)
* - val is smaller (<) than all keys (val) in right child (right)
*/
export class NodeTree {
val: number;
left: BST;
right: BST;

constructor(val: number = 0, left: BST = null, right: BST = null) {
this.val = val;
this.left = left;
this.right = right;
}
}

/**
* Examples:
* const BST0: BST = null
* const BST1: BST = new NodeTree(1, null, null)
* const BST2: BST = new NodeTree(2, null, BST1)
* const BST3: BST = new NodeTree(10, BST2, BST1)
* Template:
* fnForBST(bst: BST) {
if (bst === null) {return (...)}
else {
return (...
(bst.val)
(fnForBST(bst.left))
(fnForBST(bst.right)))
}
* }
*/

// Function Definitions:

/**
* (BST) -> number[]
* given the root, root, of a BST return all the mode(s) - if the tree has more than one mode return them in any order
* a mode is the most frequently occured element in a BST
* ASSUME: the given BST has duplicates
* Stub:
* function findMode(root: BST): number[] {return []}
* Template: <used template from>
* To do this:
* - traversal: traversing a tree means visiting and outputting the value of each node in a particular order
* - inorder traversal: traverse from the left subtree to the root then to the right subtree (Left, Root, Right)
*/
export function findMode(root: BST): number[] {
let ans: number[] = [];
let frequency: number = 0;
let maxFrequency: number = 0;
let prev: number = NaN;

function inOrderTraversal(node: BST): void {
if (node !== null) {
inOrderTraversal(node.left);

if (node.val === prev) {
frequency++;
} else {
frequency = 1;
}

if (frequency > maxFrequency) {
maxFrequency = frequency;
ans = [node.val];
} else if (frequency === maxFrequency) {
ans.push(node.val);
}
prev = node.val;

inOrderTraversal(node.right);
}
}
inOrderTraversal(root);

return ans;
}

0 comments on commit 0fcc8ae

Please sign in to comment.