【LeetCode】501. Find Mode in Binary Search Tree 解题报告(Python)
作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.cn/
题目地址:https://leetcode.com/problems/find-mode-in-binary-search-tree/#/description
题目描述
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).
题目大意
找出一个BST中出现次数最多的节点们。
解题方法
见到BST就想到中序遍历。这个题中的BST是可以包含相同的元素的,题目的要求就是找出相同的元素出现次数最多的是哪几个。那么就可以先进行中序遍历得到有序的排列,如果两个相邻的元素相同,那么这个就是连续的,找出连续最多的即可。题目思路就是BST的中序遍历加上最长连续相同子序列。
如果使用附加空间的话,可以使用hash保存每个节点出现的次数。
# Definition for a binary tree node.
# class TreeNode(object):
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution(object):
def findMode(self, root):
"""
:type root: TreeNode
:rtype: List[int]
"""
if not root: return []
self.count = collections.Counter()
self.inOrder(root)
freq = max(self.count.values())
res = []
for item, c in self.count.items():
if c == freq:
res.append(item)
return res
def inOrder(self, root):
if not root:
return
self.inOrder(root.left)
self.count[root.val] += 1
self.inOrder(root.right)
题目建议不要用附加空间hash等,方法是计算了两次,一次是统计最大的模式出现的次数,第二次的时候构建出来了数组,然后把出现次数等于最大模式次数的数字放到数组的对应位置。
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public int[] findMode(TreeNode root) {
inOrder(root);
modes = new int[modeCount];
currCount = 0;
modeCount = 0;
inOrder(root);
return modes;
}
int currVal = 0;
int currCount = 0;
int maxCount = 0;
int modeCount = 0;
int[] modes;
public void handleValue(int val){
if(currVal != val){
currVal = val;
currCount = 0;
}
currCount++;
if(currCount > maxCount){
maxCount = currCount;
modeCount = 1;
}else if (currCount == maxCount){
if(modes != null){
modes[modeCount] = currVal;
}
modeCount++;
}
}
public void inOrder(TreeNode root){
if(root == null){
return;
}
inOrder(root.left);
handleValue(root.val);
inOrder(root.right);
}
}
日期
2017 年 5 月 3 日
2018 年 11 月 23 日 —— 这就星期五了??
【LeetCode】501. Find Mode in Binary Search Tree 解题报告(Python)的更多相关文章
- 【LeetCode】669. Trim a Binary Search Tree 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 日期 题目地址:https://leetcod ...
- LeetCode: Convert Sorted List to Binary Search Tree 解题报告
Convert Sorted List to Binary Search Tree Given a singly linked list where elements are sorted in as ...
- LeetCode: Convert Sorted Array to Binary Search Tree 解题报告
Convert Sorted Array to Binary Search Tree Given an array where elements are sorted in ascending ord ...
- 【LeetCode】109. Convert Sorted List to Binary Search Tree 解题报告(Python)
[LeetCode]109. Convert Sorted List to Binary Search Tree 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id ...
- 【LeetCode】99. Recover Binary Search Tree 解题报告(Python)
[LeetCode]99. Recover Binary Search Tree 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/p ...
- 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 (找到二叉搜索树的众数)
Given a binary search tree (BST) with duplicates, find all the mode(s) (the most frequently occurred ...
- LeetCode: Lowest Common Ancestor of a Binary Search Tree 解题报告
https://leetcode.com/submissions/detail/32662938/ Given a binary search tree (BST), find the lowest ...
- 【LeetCode】235. Lowest Common Ancestor of a Binary Search Tree 解题报告(Java & Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 [LeetCode] https://leet ...
随机推荐
- Python基础之赋值与注释
目录 1. 花式赋值 1.1 链式赋值 1.2 交叉赋值 1.3 交叉赋值(解压缩) 2. 注释 2.1 单行注释 2.2 多行注释 1. 花式赋值 1.1 链式赋值 a = 10 b = 10 c ...
- EXCEL如何用公式提取一列中的唯一值和不重复值
说明:思路用的很新奇,也对COUNTIF有了更深一步的了解,但是,对于百行数据运算速度特别低,不适合数据多的使用 当面对一堆数据,我们要提取一列的唯一值的时候,如果单纯用人为一个个判断,显然是不科学的 ...
- php代码审计入门前必看
首先先介绍什么是代码审计? 代码审计:是指针对源代码进行检查,寻找代码中的bug,这是一项需要多方面技能的技术 包括:对编程的掌握,漏洞形成原理的理解,系统和中间件等的熟悉 2.为什么要进行代码审计, ...
- abuse
abuse 近/反义词: ill-treat, maltreat, mistreat, misuse, prostitute, spoil; defame, disparage, malign, re ...
- Learning Spark中文版--第四章--使用键值对(1)
本章介绍了如何使用键值对RDD,Spark中很多操作都基于此数据类型.键值对RDD通常在聚合操作中使用,而且我们经常做一些初始的ETL(extract(提取),transform(转换)和load ...
- 百度 IP 查询
查询 IP 地址以及百度爬虫 IP 我们如果要查询 IP 地址,互联网上有很多提供IP查询服务的网站,我这里总结和归纳如下: 国内提供 IP 查询的网站: IP138 IPIP,提供 IP 详细信息, ...
- 节省内存的循环banner(一)
循环banner是指scrollview首尾相连,循环播放的效果,使用非常广泛.例如淘宝的广告栏等. 如果是简单的做法可以把所有要显示的图片全部放进一个数组里,创建相同个数的图片视图来显示图片.这样的 ...
- 给webapp加上一个apk外壳
原文:http://blog.csdn.net/cmyh100/article/details/77862962 1.在Android Studio里创建一个项目 2.创建MyApplication. ...
- AOP中ProceedingJoinPoint获取目标方法,参数,注解
private void saveLog(ProceedingJoinPoint jp,long time)throws Throwable { package com.cy.pj.common.as ...
- Android Loader异步装载
一.Loader简介: (一).Loader的概念: 装载器从android3.0开始引进.它使得在activity或fragment中异步加载数据变得简单. 当成批显示数据的时候,为了使用户体验更好 ...