Simple JAVA solution using an InOrder traversal

  • 0

    The solution uses the fact that an inorder traversal of a BST produces elements in a non-decreasing order.

    low holds the value of the last accessed element.
    count_temp holds the number of occurrences of the current element.
    count holds the number of occurrences of the mode element(s).

    Two possibilities arise when traversing the tree:

    • Current element is different from the previous element. If this is true:
      reset the count_temp,
      reassign low and if count is 1,
      add the current element to the list.

    • Current element is the same as the previous element. If this is true:
      increment the count_temp;
      if count_temp exceeds the count, refresh the list, add the current element; update count.
      else, add the current element without refreshing the list.

        int low = Integer.MIN_VALUE, count = 1, count_temp = 0;    
        List<Integer> list = new ArrayList<>();
        public int[] findMode(TreeNode root) {
            int result[] = new int[list.size()];
            for(int i=0; i < list.size(); i++)
                result[i] = list.get(i);
            return result;
        public void mode(TreeNode root){
            if(root != null){
                if(root.val != low){
                    count_temp = 1;
                    low = root.val;
                    if(count == 1) list.add(root.val);                
                    if(count_temp > count){
                        list = new ArrayList<>(Arrays.asList(root.val));
                        count = count_temp;
                    else if(count_temp == count) list.add(root.val);

Log in to reply

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.