|
| 1 | +/* |
| 2 | +Problem Link: https://leetcode.com/problems/find-mode-in-binary-search-tree/ |
| 3 | +
|
| 4 | +Problem Statement: 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. |
| 5 | +
|
| 6 | +If the tree has more than one mode, return them in any order. |
| 7 | +
|
| 8 | +Assume a BST is defined as follows: |
| 9 | +
|
| 10 | +The left subtree of a node contains only nodes with keys less than or equal to the node's key. |
| 11 | +The right subtree of a node contains only nodes with keys greater than or equal to the node's key. |
| 12 | +Both the left and right subtrees must also be binary search trees. |
| 13 | +
|
| 14 | +Solution Approach: |
| 15 | +The logic lies in traversing the tree in inorder fashion and keeping a count, prev and max variable to |
| 16 | +be populated as and when required. |
| 17 | +
|
| 18 | +*/ |
| 19 | + |
| 20 | +/* ------------CODE---------------- */ |
| 21 | + |
| 22 | +/** |
| 23 | + * Definition for a binary tree node. |
| 24 | + * public class TreeNode { |
| 25 | + * int val; |
| 26 | + * TreeNode left; |
| 27 | + * TreeNode right; |
| 28 | + * TreeNode() {} |
| 29 | + * TreeNode(int val) { this.val = val; } |
| 30 | + * TreeNode(int val, TreeNode left, TreeNode right) { |
| 31 | + * this.val = val; |
| 32 | + * this.left = left; |
| 33 | + * this.right = right; |
| 34 | + * } |
| 35 | + * } |
| 36 | + */ |
| 37 | +class Solution { |
| 38 | + Integer prev = null; |
| 39 | + int count = 1; |
| 40 | + int max = 0; |
| 41 | + public int[] findMode(TreeNode root) { |
| 42 | + if(root==null) return new int[0]; |
| 43 | + |
| 44 | + List<Integer> list = new ArrayList<>(); |
| 45 | + helper(root, list); |
| 46 | + int ans[] = new int[list.size()]; |
| 47 | + for(int i=0; i<list.size(); i++) { |
| 48 | + ans[i] = list.get(i); |
| 49 | + } |
| 50 | + return ans; |
| 51 | + } |
| 52 | + |
| 53 | + private void helper(TreeNode root, List<Integer> list) { |
| 54 | + if(root==null) |
| 55 | + return; |
| 56 | + helper(root.left, list); |
| 57 | + if(prev!=null) { |
| 58 | + if(root.val == prev) |
| 59 | + count++; |
| 60 | + else count = 1; |
| 61 | + } |
| 62 | + // the logic here is beautiful, keeping a count in the order the nodes appear in a BST |
| 63 | + // and as soon as it increased the max - clear the list, and make it the new max, and add th |
| 64 | + // current node's value in the list |
| 65 | + // if we are going to get multiple nodes with same count, we can keep adding them in the list |
| 66 | + if(count > max) { |
| 67 | + max = count; |
| 68 | + list.clear(); |
| 69 | + list.add(root.val); |
| 70 | + } |
| 71 | + else if(count==max) { |
| 72 | + list.add(root.val); |
| 73 | + } |
| 74 | + prev = root.val; |
| 75 | + helper(root.right, list); |
| 76 | + } |
| 77 | +} |
| 78 | +/* |
| 79 | +Time Complexity: O(n) - to traverse all the nodes in the BST |
| 80 | +Space Complexity: O(1) [we are ignoring the stack space when traversing the tree] |
| 81 | +*/ |
0 commit comments