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


 
解题:给一个二叉查找树,返回出现次数最多的元素(众数)。并且要求不使用额外的空间,要求结果以数组的形式返回。我使用了一个ArrayList,有额外的空间消耗,虽然不满足条件,但是比较简单方便。当然也可以把数组定义为成员变量,由于数组的长度不知道,还得定义一个末尾指针,太麻烦,不如用集合。思路是,遍历BST,由于其本身的性质,遍历的过程中得到的序列,就是一个有序序列。定一个max用来保存出现做多的次数,pre记录父节点,如果没有,则为空。再定义一个res的集合保存结果,定一个cns保存当前出现最多的次数(暂时),然后不断地比较cns和max的值,随时更新res结果集,代码如下:
 /**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
ArrayList<Integer>res = null;
TreeNode pre = null;
int max = 0;
int cns = 1;
public int[] findMode(TreeNode root) {
//存放结果
res = new ArrayList<Integer>();
middle_search(root);
int[] arr =new int[res.size()];
for(int i =0; i < arr.length; i++)
arr[i] = res.get(i);
return arr;
}
public void middle_search(TreeNode root) {
if(root == null)
return ;
middle_search(root.left);
//处理根结点
if(pre != null){ //有父节点
if(root.val == pre.val)
cns++;
else cns = 1;
}
if(cns >= max){
if(cns > max)
res.clear();
max = cns;
res.add(root.val);
}
pre = root; middle_search(root.right);
}
}
 
 

501. Find Mode in Binary Search Tree【LeetCode by java】的更多相关文章

  1. 35. leetcode 501. Find Mode in Binary Search Tree

    501. Find Mode in Binary Search Tree Given a binary search tree (BST) with duplicates, find all the  ...

  2. 【LeetCode】501. Find Mode in Binary Search Tree 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

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

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

  4. 501. Find Mode in Binary Search Tree

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

  5. 85. Insert Node in a Binary Search Tree【easy】

    Given a binary search tree and a new tree node, insert the node into the tree. You should keep the t ...

  6. 501. Find Mode in Binary Search Tree查找BST中的众数

    [抄题]: Given a binary search tree (BST) with duplicates, find all the mode(s) (the most frequently oc ...

  7. 【LeetCode】235. Lowest Common Ancestor of a Binary Search Tree 解题报告(Java & Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 [LeetCode] https://leet ...

  8. Binary Search Tree Iterator——LeetCode

    Implement an iterator over a binary search tree (BST). Your iterator will be initialized with the ro ...

  9. Binary Search Tree Iterator leetcode

    Implement an iterator over a binary search tree (BST). Your iterator will be initialized with the ro ...

随机推荐

  1. 树莓派踩坑备忘录 -- 使用 Linux

    目录 一,工欲善其事,必先利其器 二,开机必备 三,更新 apt-get 源与软件搜索 四,安装 .NET Core 五,文件传输 六,搜索与安装软件 七,常见缺少的 xxx.so 八,小技巧与工具 ...

  2. Azure IoT 预配置解决方案

    Azure IoT 预配置解决方案 Sangyu Li © 2018  一.什么是Azure IoT 预配置解决方案? 如图,这就是Azure IoT Suite中 Provision solutio ...

  3. spark任务调度和资源分配

    Spark调度模式 FIFO和FAIR     Spark中的调度模式主要有两种:FIFO和FAIR.    默认情况下Spark的调度模式是FIFO(先进先出),谁先提交谁先执行,后面的任务需要等待 ...

  4. python提示警告InsecureRequestWarning

    在Python3中使用以下代码报错: import requests response = requests.get(url='', verify=False) 错误代码如下: InsecureReq ...

  5. 【Java集合源代码剖析】ArrayList源代码剖析

    版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/mmc_maodun/article/details/35568011 转载请注明出处:http:// ...

  6. Object Detection API 相关

    训练官方提供的数据集: http://blog.csdn.net/LiJiancheng0614/article/details/77756252 训练自己的数据集(墙外): https://medi ...

  7. day3-作业及答案

    作业:1.用python实现冒泡排序# [50,20,30,10]## 升序:谁大谁交换到后面# 降序:谁大谁交换到前面## 以升序为例# 第1趟:# [20,50,30,10]# [20,30,50 ...

  8. pyspider爬取数据存入es--1.安装驱动

    跟使用mysql一样,不安装es驱动的话,也会触发模块找不到的错误 ImportError: No module named elasticsearch 通过pip安装 pip install ela ...

  9. 浅谈 DNS

    一.DNS(Domain Name System,域名系统) 概念:万维网(WWW是环球信息网的缩写,亦作“Web”.“WWW”.“'W3'”,英文全称为“World Wide Web”),作为域名和 ...

  10. Flask 入门一( flask 框架和 flask-script 库)

    Flask 入门一( flask 框架 和 flask-script 库) 一.Flask框架: 1.简介 Flask是一个非常小的Python Web框架,被称为微型框架:只提供了一个稳健的核心,其 ...