Skip to content

Commit

Permalink
Create 501_Find_Mode_in_Binary_Search_Tree.java
Browse files Browse the repository at this point in the history
  • Loading branch information
mkhuzaima committed Nov 2, 2023
1 parent bf294a6 commit 8b50cd9
Showing 1 changed file with 62 additions and 0 deletions.
62 changes: 62 additions & 0 deletions 501_Find_Mode_in_Binary_Search_Tree.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
// id: 501
// Name: Find Mode in Binary Search Tree
// link: https://leetcode.com/problems/find-mode-in-binary-search-tree/
// Difficulty: Easy

/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {

Map<Integer, Integer> map = new HashMap<>();

public int[] findMode(TreeNode root) {
dfs(root);

// get max from node;
int max = 0;
int frequency = 0;
for (int key: map.keySet()) {
if (map.get(key) > max) {
max = map.get(key);
frequency = 1;
}
else if (map.get(key) == max) {
frequency++;
}
}

// get all nodes with frequency = max
int [] ans = new int[frequency];
int i = 0;

for (int key: map.keySet()) {
if (map.get(key) == max) {
ans[i++] = key;
}
}


return ans;
}

private void dfs(TreeNode node) {
if (node== null) return;

dfs(node.left);
dfs(node.right);
map.put(node.val, map.getOrDefault(node.val, 0)+1);
}
}

0 comments on commit 8b50cd9

Please sign in to comment.