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 ...
随机推荐
- cd mkdir mv cp rm 命令目录相关操作
切换目录: cd 家目录 cd. 当前目录 cd.. 当前上一级目录 cd../../当前目录的上上级目录 cd - 返回前一个目录 --------------------------------- ...
- C# Excel操作类 ExcelHelper
实现C#与Excel文件的交互操作,实现以下功能: 1.DataTable 导出到 Excel文件 2.Model数据实体导出到 Excel文件[List<Model>] 3.导出数据到模 ...
- DevExpress 使用条形码二维码控件打印
参考文章: https://www.cnblogs.com/wuhuacong/p/6112976.html 转载请注明出处:撰写人:伍华聪 其实主要是二维码的实现,在使用条形码控件时,又一个属性Sy ...
- SDN第三次上机
1.创建以下拓扑(可采用任意方式) 2.利用OVS命令下发流表,实现VLAN功能 3.利用OVS命令查看流表 4.验证性测试 5.Wireshark抓包验证
- python第三十课--异常(finally讲解)
演示:finally语句的作用 try: fr="" path=r'kaifanglist1.txt' fr=open(path,encoding='utf-8') print(f ...
- BZOJ3632:外太空旅行(最大团,DFS)
Description 在人类的触角伸向银河系的边缘之际,普通人上太空旅行已经变得稀松平常了.某理科试验班有n个人,现在班主任要从中选出尽量多的人去参加一次太空旅行活动. 可是n名同学并不是和平相处的 ...
- BZOJ3233:[AHOI2013]找硬币(DP)
Description 小蛇是金融部部长.最近她决定制造一系列新的货币.假设她要制造的货币的面值为x1,x2,x3… 那么x1必须为1,xb必须为xa的正整数倍(b>a).例如 1,5,125, ...
- android 登录效果
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android=&q ...
- ethjs-1-了解
https://github.com/ethjs/ethjs/blob/master/docs/user-guide.md Install npm install --save ethjs Usage ...
- ICC Stage Flow
initial: reference library(mw), link library(db), create_mw_lib, read_verilog, link create_floorplan ...