501. Find Mode in Binary Search Tree【LeetCode by java】
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).
/**
* 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】的更多相关文章
- 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 ...
- 【LeetCode】501. Find Mode in Binary Search Tree 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 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 ...
- 501. Find Mode in Binary Search Tree
Given a binary search tree (BST) with duplicates, find all the mode(s) (the most frequently occurred ...
- 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 ...
- 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 ...
- 【LeetCode】235. Lowest Common Ancestor of a Binary Search Tree 解题报告(Java & Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 [LeetCode] https://leet ...
- Binary Search Tree Iterator——LeetCode
Implement an iterator over a binary search tree (BST). Your iterator will be initialized with the ro ...
- Binary Search Tree Iterator leetcode
Implement an iterator over a binary search tree (BST). Your iterator will be initialized with the ro ...
随机推荐
- PyQt5--StatusBar
# -*- coding:utf-8 -*- ''' Created on Sep 13, 2018 @author: SaShuangYiBing ''' import sys from PyQt5 ...
- python创建目录保存文件
创建目录 在Python中可以使用os.mkdir()函数创建目录(创建一级目录). 其原型如下所示: os.mkdir(path) 其参数path 为要创建目录的路径. 例如要在D盘下创建hello ...
- 2.HBase In Action 第一章-HBase简介(1.1数据管理系统:快速学习)
Relational database systems have been around for a few decades and have been hugely successful in so ...
- [JSOI2009]球队收益
题目 这题好神啊 我们发现一个球队的总比赛场数是确定的,设第\(i\)支球队一共进行了\(s_i\)场比赛 于是这个球队的收益就是\(c_i\times x^2+d_i(s_i-x)^2\) 我们拆开 ...
- Android Environment 获取各种路径的方法
<pre name="code" class="java">package com.deepoon.beyond.environment; impo ...
- leetcode-856 Score of Parentheses
Given a balanced parentheses string S, compute the score of the string based on the following rule: ...
- 输入5个学生的信息(包括学号,姓名,英语成绩,计算机语言成绩和数据库成绩), 统计各学生的总分,然后将学生信息和统计结果存入test.txt文件中
题目分析: 1.首先想到的是数组存放数据,数组肯定是String类型. 2.String类型的数组,5行6列.要把从第0行第2列到第4行第4列的数据取出转换成数值型,再统计三科总分.最后把计算出的总分 ...
- 6-51单片机ESP8266学习-AT指令(8266TCP服务器--做自己的AndroidTCP客户端发信息给单片机控制小灯的亮灭)
http://www.cnblogs.com/yangfengwu/p/8776712.html 先把源码和资料链接放到这里 链接: https://pan.baidu.com/s/1jpHZjW_7 ...
- day59
轮播图作业 <!DOCTYPE html> <html lang="zh"> <head> <meta charset="UTF ...
- Oracle 解决【ORA-01704:字符串文字太长】
错误提示:oracle在toad中执行一段sql语句时,出现错误‘ORA-01704:字符串文字太长’.如下图: 原因:一般为包含有对CLOB字段的数据操作.如果CLOB字段的内容非常大的时候,会导致 ...