|
| 1 | +/** |
| 2 | +* Given a binary search tree (BST) with duplicates, find all the mode(s) |
| 3 | +* (the most frequently occurred element) in the given BST. |
| 4 | +* |
| 5 | +* Assume a BST is defined as follows: |
| 6 | +* - The left subtree of a node contains only nodes with keys less than or equal to the node's key. |
| 7 | +* - The right subtree of a node contains only nodes with keys greater than or equal to the node's key. |
| 8 | +* - Both the left and right subtrees must also be binary search trees. |
| 9 | +* For example: |
| 10 | +* Given BST [1,null,2,2], |
| 11 | +* |
| 12 | +* 1 |
| 13 | +* \ |
| 14 | +* 2 |
| 15 | +* / |
| 16 | +* 2 |
| 17 | +* |
| 18 | +* return [2]. |
| 19 | +* |
| 20 | +* Note: If a tree has more than one mode, you can return them in any order. |
| 21 | +* |
| 22 | +* Follow up: Could you do that without using any extra space? (Assume that the |
| 23 | +* implicit stack space incurred due to recursion does not count). |
| 24 | +*/ |
| 25 | + |
| 26 | +/** |
| 27 | +* Definition for a binary tree node. |
| 28 | +* public class TreeNode { |
| 29 | +* int val; |
| 30 | +* TreeNode left; |
| 31 | +* TreeNode right; |
| 32 | +* TreeNode(int x) { val = x; } |
| 33 | +* } |
| 34 | +*/ |
| 35 | + |
| 36 | +public class FindModeInBinarySearchTree501 { |
| 37 | +public int[] findMode(TreeNode root) { |
| 38 | +Result result = new Result(); |
| 39 | +findMode(root, result); |
| 40 | +int[] output = new int[result.res.size()]; |
| 41 | +int i = 0; |
| 42 | +for (int r: result.res) { |
| 43 | +output[i++] = r; |
| 44 | +} |
| 45 | +return output; |
| 46 | +} |
| 47 | + |
| 48 | +private void findMode(TreeNode root, Result result) { |
| 49 | +if (root == null) return; |
| 50 | + |
| 51 | +findMode(root.left, result); |
| 52 | + |
| 53 | +if (result.maxCount == -1) { |
| 54 | +result.maxCount = 1; |
| 55 | +result.currCount = 1; |
| 56 | +result.curr = root.val; |
| 57 | +result.res = new HashSet<>(); |
| 58 | +result.res.add(root.val); |
| 59 | +} else if (result.curr != root.val) { |
| 60 | +result.currCount = 1; |
| 61 | +result.curr = root.val; |
| 62 | +if (result.maxCount == result.currCount) { |
| 63 | +result.res.add(root.val); |
| 64 | +} |
| 65 | +} else { |
| 66 | +result.currCount++; |
| 67 | +if (result.maxCount == result.currCount) { |
| 68 | +result.res.add(root.val); |
| 69 | +} else if (result.maxCount < result.currCount) { |
| 70 | +result.maxCount = result.currCount; |
| 71 | +result.res = new HashSet<>(); |
| 72 | +result.res.add(root.val); |
| 73 | +} |
| 74 | +} |
| 75 | + |
| 76 | +findMode(root.right, result); |
| 77 | +} |
| 78 | + |
| 79 | +class Result { |
| 80 | +int maxCount; |
| 81 | +int currCount; |
| 82 | +int curr; |
| 83 | +Set<Integer> res; |
| 84 | +Result() { |
| 85 | +this.maxCount = -1; |
| 86 | +this.currCount = -1; |
| 87 | +this.curr = 0; |
| 88 | +this.res = new HashSet<>(); |
| 89 | +} |
| 90 | +} |
| 91 | + |
| 92 | + |
| 93 | +/** |
| 94 | +* https://leetcode.com/problems/find-mode-in-binary-search-tree/discuss/98101/Proper-O(1)-space |
| 95 | +*/ |
| 96 | +public int[] findMode2(TreeNode root) { |
| 97 | +inorder(root); |
| 98 | +modes = new int[modeCount]; |
| 99 | +modeCount = 0; |
| 100 | +currCount = 0; |
| 101 | +inorder(root); |
| 102 | +return modes; |
| 103 | +} |
| 104 | + |
| 105 | +private int currVal; |
| 106 | +private int currCount = 0; |
| 107 | +private int maxCount = 0; |
| 108 | +private int modeCount = 0; |
| 109 | + |
| 110 | +private int[] modes; |
| 111 | + |
| 112 | +private void handleValue(int val) { |
| 113 | +if (val != currVal) { |
| 114 | +currVal = val; |
| 115 | +currCount = 0; |
| 116 | +} |
| 117 | +currCount++; |
| 118 | +if (currCount > maxCount) { |
| 119 | +maxCount = currCount; |
| 120 | +modeCount = 1; |
| 121 | +} else if (currCount == maxCount) { |
| 122 | +if (modes != null) |
| 123 | +modes[modeCount] = currVal; |
| 124 | +modeCount++; |
| 125 | +} |
| 126 | +} |
| 127 | + |
| 128 | +private void inorder(TreeNode root) { |
| 129 | +if (root == null) return; |
| 130 | +inorder(root.left); |
| 131 | +handleValue(root.val); |
| 132 | +inorder(root.right); |
| 133 | +} |
| 134 | + |
| 135 | +} |
0 commit comments