Class Solution

  • All Implemented Interfaces:

    
    public final class Solution
    
                        

    501 - Find Mode in Binary Search Tree.

    Easy

    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 <code>1, 10<sup>4</sup></code>.

    • <code>-10<sup>5</sup><= Node.val <= 10<sup>5</sup></code>

    Follow up: Could you do that without using any extra space? (Assume that the implicit stack space incurred due to recursion does not count).

    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
    • Field Summary

      Fields 
      Modifier and Type Field Description
    • Constructor Summary

      Constructors 
      Constructor Description
      Solution()
    • Enum Constant Summary

      Enum Constants 
      Enum Constant Description
    • Method Summary

      Modifier and Type Method Description
      final IntArray findMode(TreeNode root)
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait