Find Mode in Binary Search Tree – LeetCode 501
Problem
Description
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.
Answer
Original
Code
1 | /** |
思路
使用中序遍历来提供对原有大小顺序的保存,于是可以在One Pass情况下统计出现次数。时间复杂度$O(n)$,空间复杂度$O(1)$。
耗时$14$ ms,排名$0.57\%$
Better
思路
还没看到更好的思路。