Skip to content

Instantly share code, notes, and snippets.

@smatthewenglish
Created November 1, 2023 16:14
Show Gist options
  • Save smatthewenglish/e2b58883298c06acd546576cd195ac85 to your computer and use it in GitHub Desktop.
Save smatthewenglish/e2b58883298c06acd546576cd195ac85 to your computer and use it in GitHub Desktop.
501. Find Mode in Binary Search Tree
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, 104].
-105 <= Node.val <= 105
Follow up: Could you do that without using any extra space? (Assume that the implicit stack space incurred due to recursion does not count).
@smatthewenglish
Copy link
Author

/**
 * 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 {

    Integer max = 0;
    HashMap<Integer, Integer> modeMap = new HashMap<>();

    public int[] findMode(TreeNode root) {

        int value = root.val;

        if(modeMap.containsKey(value)) {
            
            Integer countKey = modeMap.get(value) + 1;
            modeMap.put(value, countKey);
            if(countKey > max) {
                max = countKey;
            }

        } else {
            modeMap.put(value, 1);
            max = 1;
        }

        if(root.left != null) {
            findMode(root.left);
        }
        if (root.right != null) {
            findMode(root.right);
        }

        HashSet<Integer> resultSet = new HashSet<>();
        modeMap.forEach((key, val) -> {
            if (val.equals(max)) {
                resultSet.add(key);
            }
        });
        int[] output = resultSet.stream().mapToInt(Integer::intValue).toArray();
        return output;
    }
}

@smatthewenglish
Copy link
Author

            System.out.println("maxFrequency: " + maxFrequency);
            System.out.println("         key: " + key);

@smatthewenglish
Copy link
Author

/**
 * 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 {

    Integer maxFrequency = 0;
    HashMap<Integer, Integer> modeMap = new HashMap<>();

    public int[] findMode(TreeNode root) {

        Integer key = root.val;
        Integer countKey = 0;

        if(modeMap.containsKey(key)) {
            countKey = modeMap.get(key) + 1;
            modeMap.put(key, countKey);
        } else {
            countKey = 1;
            modeMap.put(key, countKey);
        }

        if(countKey > maxFrequency) {
            maxFrequency = countKey;
        }

        if(root.left != null) {
            findMode(root.left);
        }
        if (root.right != null) {
            findMode(root.right);
        }

        HashSet<Integer> resultSet = new HashSet<>();
        modeMap.forEach((keyMap, countKeyMap) -> {
            if (countKeyMap.equals(maxFrequency)) {
                resultSet.add(keyMap);
            }
        });
        int[] output = resultSet.stream().mapToInt(Integer::intValue).toArray();
        return output;
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment