-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFindModeInBinarySearchTree.ts
137 lines (120 loc) · 3.62 KB
/
FindModeInBinarySearchTree.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
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;
}