-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create 501_Find_Mode_in_Binary_Search_Tree.java
- Loading branch information
Showing
1 changed file
with
62 additions
and
0 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
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); | ||
} | ||
} |