501. Find Mode in Binary Search Tree

[copy title]:

Given a binary search tree (BST) with duplicates, find all the mode(s) (the most frequently occurred element) in the given BST.

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.

 

For example:
Given BST [1,null,2,2],

   1
    \
     2
    /
   2

 

return [2].

 [brute force solution]:

Time analysis:

Spatial analysis: hashmap:n

 [Optimized]:

Time analysis:

Spatial analysis: various counts

[Wonderful output conditions]:

Returns the specific element, not the count. So in turn nums[number of times] = elements.

[Wonderful corner case]:

[Thinking question]:

I don't know how big the specified space of the new array is, so I need to traverse it in advance

[One sentence idea]:

[Input amount]: Empty: Normal situation: Extra large: Extra small: Special situations handled in the program: Abnormal situations (Illegal and unreasonable input):

[Paint]:

[One brush]:

[Second brush]:

[Three brushes]:

[Four brushes]:

[Five brushes]:

  [Results of five-minute naked eye debug]:

[Summarize]:

[Complexity]: Time complexity: O( ) Space complexity: O( )

[English data structures or algorithms, why not use other data structures or algorithms]:

[Key templating code]:

[Other solutions]:

[Follow Up]:

[The topics given by LC change and change]:

 [Code style]:

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325056401&siteId=291194637