leetcood学习笔记-501- 二叉搜索树中的众数
题目描述:

方法一:
class Solution:
def findMode(self, root: TreeNode) -> List[int]:
if not root:
return []
dic = {}
stack = [root]
while stack:
node = stack.pop()
if node.val not in dic:
dic[node.val] = 0
dic[node.val] += 1
if node.left:
stack.append(node.left)
if node.right:
stack.append(node.right)
re = []
max_v = max(dic.values())
for key,val in dic.items():
if val == max_v:
re.append(key)
return re
方法二:
class Solution:
def findMode(self, root):
"""
:type root: TreeNode
:rtype: List[int]
"""
cur_val = -10**15
cur_fre = 0
ans = []
fre_max = 0
def find_fre(p):
nonlocal cur_val#nonlocal关键字用来在函数或其他作用域中使用外层(非全局)变量
nonlocal cur_fre
nonlocal ans
nonlocal fre_max
if not p:
return
find_fre(p.left)
if cur_val == p.val:
cur_fre += 1
else:
cur_val = p.val
cur_fre = 1
if cur_fre == fre_max:
ans.append(cur_val)
if cur_fre > fre_max:
fre_max = cur_fre
ans.clear()
ans.append(cur_val)
find_fre(p.right) find_fre(root)
return ans
leetcood学习笔记-501- 二叉搜索树中的众数的更多相关文章
- Java实现 LeetCode 501 二叉搜索树中的众数
501. 二叉搜索树中的众数 给定一个有相同值的二叉搜索树(BST),找出 BST 中的所有众数(出现频率最高的元素). 假定 BST 有如下定义: 结点左子树中所含结点的值小于等于当前结点的值 结点 ...
- [Swift]LeetCode501. 二叉搜索树中的众数 | Find Mode in Binary Search Tree
Given a binary search tree (BST) with duplicates, find all the mode(s) (the most frequently occurred ...
- Leetcode501.Find Mode in Binary Search Tree二叉搜索树中的众数
给定一个有相同值的二叉搜索树(BST),找出 BST 中的所有众数(出现频率最高的元素). 假定 BST 有如下定义: 结点左子树中所含结点的值小于等于当前结点的值 结点右子树中所含结点的值大于等于当 ...
- LeetCode501.二叉搜索树中的众数
题目,本题未做出,还有很多要学习 class Solution { public: vector<int>ans; int base,count,maxCount; void update ...
- [LeetCode] Delete Node in a BST 删除二叉搜索树中的节点
Given a root node reference of a BST and a key, delete the node with the given key in the BST. Retur ...
- [Swift]LeetCode450. 删除二叉搜索树中的节点 | Delete Node in a BST
Given a root node reference of a BST and a key, delete the node with the given key in the BST. Retur ...
- [Swift]LeetCode701. 二叉搜索树中的插入操作 | Insert into a Binary Search Tree
Given the root node of a binary search tree (BST) and a value to be inserted into the tree, insert t ...
- [LeetCode] Insert into a Binary Search Tree 二叉搜索树中插入结点
Given the root node of a binary search tree (BST) and a value to be inserted into the tree, insert t ...
- 230. 二叉搜索树中第K小的元素
230. 二叉搜索树中第K小的元素 题意 给定一个二叉搜索树,编写一个函数 kthSmallest 来查找其中第 k 个最小的元素. 你可以假设 k 总是有效的,1 ≤ k ≤ 二叉搜索树元素个数. ...
- leetcode 二叉搜索树中第K小的元素 python
二叉搜索树中第K小的元素 给定一个二叉搜索树,编写一个函数 kthSmallest 来查找其中第 k 个最小的元素. 说明:你可以假设 k 总是有效的,1 ≤ k ≤ 二叉搜索树元 ...
随机推荐
- GitHub-Hexo-Blog 集成Gitalk评论插件
在本文)末尾可查看先查看效果: 1. 新建New OAuth App 在github中,Settings / Develpoer settings OAuth Apps / New OAuth App ...
- 关于 群晖 docker 百度云盘下载的使用心得
因为有了群晖,所以想折腾一下看看有什么更多的功能,今天就来折腾一下群晖百度云盘下载.毕竟现在云盘都限速了嘛... 在群晖里,要想用到百度云盘下载,就需要有个小东西,就是docker.docker很简单 ...
- Spring Boot 2.0 常见问题总结(一)
SpringBoot2.x 依赖环境和版本新特性说明 依赖版本 jdk8 以上, Springboot2.x 用 JDK8 , 因为底层是 Spring framework5 . jar 包方式运行 ...
- 【Movist Pro】macOS上的绝佳媒体播放器
Movist Pro是适用于Mac的高性能电影播放器,如果比较流程和界面,则Movist与QuickTime非常相似.因此,采用播放器几乎不会有任何问题.使用Quicktime或FFmpeg解码电影并 ...
- JDK1.8之后的新特性和新接口
接口的旧特性: 就特性下接口中只有: 常量(必须赋值) 抽象方法abstract(和final static private三个关键字冲突) interface Inter { //int a ; / ...
- asp.net core Mvc 增删改查
1.创建项目 创建Data文件夹 创建实体类Students/cs public class Students { public Guid Id { get; set; } public string ...
- Thinkphp5获取文件上传信息
Thinkphp5内置有处理文件上传的方法,因在开发文档没有找到获取上传文件基本信息的说明,故在这里做一下记录. $file = request()->file('input类型为file的na ...
- js关闭当前窗口的几种方法
第一种:不带任何提示关闭窗口的js代码 <a href="javascript:window.opener=null;window.open('','_self');window.cl ...
- Unity Document
{ https://docs.unity3d.com/ScriptReference/Events.UnityEventTools.html }
- Shell4
ssh 192.168.4.5>提示continue,连接过的主机不会提示>连接过的主机 文件存放位置:/root/.ssh/known_hosts ################### ...