【LeetCode】第501题——二叉搜索树中的众数(难度:简单)
题目描述
给定一个有相同值的二叉搜索树(BST),找出 BST 中的所有众数(出现频率最高的元素)。
假定 BST 有如下定义:
结点左子树中所含结点的值小于等于当前结点的值
结点右子树中所含结点的值大于等于当前结点的值
左子树和右子树都是二叉搜索树
-
示例:
输入: BST [1,null,2,2],1 \ 2 / 2
输出:[2].
提示:如果众数超过1个,不需考虑输出顺序
进阶:你可以不使用额外的空间吗?(假设由递归产生的隐式调用栈的开销不被计算在内)
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/find-mode-in-binary-search-tree
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
解题思路
遍历方式都是基于中序遍历。
思路一:普通中序遍历,利用HashMap存储出现过的val值和其出现次数。在HashMap中找出众数。
思路二:普通中序遍历,利用BST的特性,中序遍历到的数字是按顺序递增的。也就是说如果当前数字等于上一个数字,则 ++count,若当前数字不等于上一数字,则count=1。然后比较count和max(max是最大出现次数)
思路三:Morris中序遍历,在加上思路二的存储最大值思路。
这里简单说一下Morris中序遍历的步骤,因为树的遍历结合动图会更容易理解,但本人技术有限,因此具体理论不再此阐述。
- 如果cur无左子树,cur向右移动(cur=cur.right)
- 如果cur有左子树,找到cur左子树上最右的节点,记为rightend
- 如果rightend的right指针指向空,让其(rightend的right指针)指向cur,cur向左移动(cur=cur.left)
- 如果rightend的right指针指向cur,让其(rightend的right指针)指向空,cur向右移动(cur=cur.right)
代码详解
思路一:中序遍历+HashMap
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
HashMap<Integer, Integer> hm;
int max = 1; // 0也可
public int[] findMode(TreeNode root) {
hm = new HashMap<>();
Stack<Integer> stack = new Stack<>(); // 利用栈来存储复数个众数
func(root); // 中序遍历的递归写法
// 递归完后max也会得到,遍历HashMap的key,找到value==max的key,压入stack中
for(int i : hm.keySet()) {
if(hm.get(i) == max) {
stack.push(i);
}
}
// stack的作用其实就是得到ints数组的长度
int[] ints = new int[stack.size()];
for(int i = 0; i < ints.length; ++i) {
ints[i] = stack.pop();
}
return ints;
}
// 这个func的作用是中序遍历+更新HashMap+更新max
public void func(TreeNode node) {
if(node != null) {
// 更新HashMap
if(hm.containsKey(node.val)) {
hm.replace(node.val, hm.get(node.val)+1);
} else {
hm.put(node.val, 1);
}
// 更新max
if(hm.get(node.val) > max) {
max = hm.get(node.val);
}
// 中序遍历
func(node.left);
func(node.right);
}
}
}
思路二:中序遍历(但不用记录所有val的出现次数)
class Solution {
Stack<Integer> stack;
int max = 0; // 同一val出现的最大次数
int base = 1; // 上一个数,判断当前数与上一个数是否相等,初值无所谓是多少
int count = 0; // 记录当前数的出现次数,换了个base就得重置一遍count=1
public int[] findMode(TreeNode root) {
stack = new Stack<>();
func(root); // 中序遍历
// stack已经在func中确定了,依次赋值给ints即可
int[] ints = new int[stack.size()];
for(int i = 0; i < ints.length; ++i) {
ints[i] = stack.pop();
}
return ints;
}
// 这个func的作用是中序遍历+更新count+更新base+更新max+更新stack
public void func(TreeNode node) {
if(node != null) {
// 中序遍历先是左
func(node.left);
// 中序遍历再是根
if(node.val == base) { // 当前数字和上一数字相同,则++count
++count;
} else { // 若不同则重置count=1并更改base
count = 1;
base = node.val;
}
// 若max变了,stack就得清除一遍,再压入当前遍历的数字
if(count > max) {
max = count;
stack.clear();
stack.push(node.val);
} else if(count == max) { // 别忘了出现次数等于max的数字也需要入栈
stack.push(node.val);
}
// 中序遍历最后是右
func(node.right);
}
}
}
思路三:Morris中序遍历+思路二
class Solution {
// 这里与思路二一致
Stack<Integer> stack;
int max = 0;
int base = 1;
int count = 0;
public int[] findMode(TreeNode root) {
stack = new Stack<>();
// Morris中序遍历思路若有不懂则返回上方解题思路章节
TreeNode cur = root; // 前驱节点cur
TreeNode rightend = null; // 左子树最右的节点rightend
// Morris中序遍历本体
// 在新的遍历方法学会之后,还得知道什么时候更新max、base、count、stack
// 有一种情况比较好理解:遍历到最左的时候对cur进行更新,这点和普通中序遍历大体类似
// 还有一种情况:rightend的right指针指向cur时,对cur进行更新,也就是返回前驱节点时进行更新
while(cur != null) {
// 如果cur无左子树,cur向右移动(cur=cur.right)
if(cur.left == null) {
func(cur);
cur = cur.right;
// 如果cur有左子树,找到cur左子树上最右的节点,记为rightend
} else {
rightend = cur.left;
while(rightend.right != null && rightend.right != cur) {
rightend = rightend.right;
}
// 如果rightend的right指针指向空,让其指向cur,cur向左移动(cur=cur.left)
if(rightend.right == null) {
rightend.right = cur;
cur = cur.left;
// 如果rightend的right指针指向cur,让其指向空,cur向右移动(cur=cur.right)
} else if (rightend.right == cur) {
func(cur);
rightend.right = null;
cur = cur.right;
}
}
}
// stack里存储的都是众数了
int[] ints = new int[stack.size()];
for(int i = 0; i < ints.length; ++i) {
ints[i] = stack.pop();
}
return ints;
}
// 这个func的作用是更新count+更新base+更新max+更新stack
// 说白了就是把思路二的左右子树的递归给删去了
// 因为主程序的外循环是while(cur != null),因此实参一定不是null,不用考虑null的情况了
public void func(TreeNode node) {
if(node.val == base) {
++count;
} else {
count = 1;
base = node.val;
}
if(count > max) {
max = count;
stack.clear();
stack.push(node.val);
} else if(count == max) {
stack.push(node.val);
}
}
}
注意点
- 个人认为思路二就很可以了,再进阶一点可以再额外掌握Morris中序遍历。