LeetCode 501. Find Mode in Binary Search Tree (找到二叉搜索树的众数)

时间:2022-04-08 12:05:13

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].

Note: If a tree has more than one mode, you can return them in any order.

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


题目标签:Tree
  这道题目给了我们一个二叉搜索树,让我们找到树的众树 (出现最多的那个值),可以是一个众树,也可以有很多个。看完题目第一个想到用HashMap,但是发现题目最后follow up说不能用extra space。所以我们要另外考虑方法。二叉搜索树的特性,左边 <= 根 <= 右边,这道题目包括了等于。举一个例子:
                      10
                       /      \
                                                                            5       13
                      /   \         \
                                                                        3     7       13
                                                                       /  \      \  
                                                                     2    3     9
   看这个例子,我们试着把它上下压缩一下, 就等于, 2 3 3 5 7 9 10 13 13 ,在纸上画的左右分开比较容易看清。发现这是一个有序的排列。如果我们可以遍历这个顺序的话,它是从小到大的,特点就是,一样的数字一定是连在一起的。这样我们就可以设一个count = 1,一个maxCount = 0 和一个pre Node, count是记录每一个数字的连续出现次数,如果大于maxCount 那就说明这个数字是新的mode,比起之前的数字。 maxCount 记录最大出现次数的mode。pre Node是上一个点的数字,当每次current 点和上一个点比较,是否两个数字相同,来判断需要count++,如果不相同,那就更新count = 1。
  如何得到这个有序排列,可以通过inorder traversal 来实现,需要注意的是, Java 是 Pass by Value 的核心,所以pre node , count 什么的,需要放在function 外面。

Java Solution:

Runtime beats 74.31%

完成日期:07/07/2017

关键词:Tree

关键点:inorder 遍历 (从小到大顺序)

 /**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution
{
TreeNode pre = null;
int cnt = 1;
int max_cnt = 0; public int[] findMode(TreeNode root)
{
ArrayList<Integer> res = new ArrayList<>(); inorder(root, res); int[] result = new int[res.size()]; for(int i=0; i<result.length; i++)
result[i] = res.get(i); return result;
} public void inorder(TreeNode node, ArrayList<Integer> res)
{
if(node == null)
return; inorder(node.left, res);
// meaning this node has a previous node, need to compare them to determine cnt
if(pre != null)
{
if(node.val == pre.val) // if this node has same value as pre's
cnt++;
else // if this node has different value as pre's
cnt = 1;
} // once cnt is greater max_cnt, meaning find a new mode, need to clear res;
if(cnt > max_cnt)
{
max_cnt = cnt;
res.clear();
res.add(node.val);
}
else if(cnt == max_cnt) // cnt == max_cnt, meaning find a new mode that equal to pre mode.
res.add(node.val); if(pre == null) // for first most left leaf node, its pre is null, set the first pre node
{
pre = new TreeNode(node.val);
}
else // if pre is not null, update this node's val each time
pre.val = node.val; inorder(node.right, res);
}
}

参考资料:

http://www.cnblogs.com/grandyang/p/6436150.html

LeetCode 算法题目列表 - LeetCode Algorithms Questions List