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 ≤ 二叉搜索树元 ... 
随机推荐
- Arch安装墨刀(产品原型工具)
			Arch通过aur安装墨刀的时候报错,查看PKGBUILD发现下载地址错误("https://s3.cn-north-1.amazonaws.com.cn/modao/download&qu ... 
- CICS FILE OPEN
			CEMT I CECD V FILE() GROUP() CEDA check error log in JESYSMSG FILE OPEN/CLOSE STATUS CICS ACTION res ... 
- 笔记67 Spring Boot快速入门(七)
			SpringBoot+RESTful+JSON 一.RESTful架构 REST全称是Representational State Transfer,中文意思是表述(编者注:通常译为表征)性状态转移. ... 
- visual studio 自定义警告标签
			写代码中经常会遇见某段代码写的不大合适或者是有隐患,但是一时半会有没时间不去完善,总体上不影响程序,也就放下了 结果时间一久就给忘了 vs提供了自定义警告的功能,这样就能有个提醒啦,方便以后改进 us ... 
- 微信小程序分享功能的path路径
			表示被微信小程序坑惨了 糟心的开始写,老板说先不上太复杂,就先显示了两个页面,然后开开心心的发布,测试了几遍,没啥问题呀.结果,一上上去,就被老板批了! 啥呀! 这分享怎么这个鬼样子!明明我看文档都是 ... 
- 图像元数据编辑软件:MetaImage使用流程讲解
			MetaImage是唯一的macOS工具,允许在处理所有类型的标签格式时编辑,读取和写入元数据.在时尚的界面中导航,您可以更改图像元数据的所有内容,并对数百张相似的照片进行相同的编辑. https:/ ... 
- QDomDocument::clear()的调用,会导致关闭程序时崩溃!!!
			//读一份xml前,先清理m_Doc[QDomDocument] bool XmlIO::xmlRead(QString &errmsg) { m_mutex.lock(); // m_Doc ... 
- RAM SSO功能重磅发布 —— 满足客户使用企业本地账号登录阿里云
			阿里云RAM (Resource Access Management)为客户提供身份与访问控制管理服务.使用RAM,可以轻松创建并管理您的用户(比如雇员.企业开发的应用程序),并控制用户对云资源的访问 ... 
- ios移动输入框被软键盘遮挡
			页面输入框会出现被软键盘挡住的问题: 解决方法:获取input点击事件设置body高度 $('input').bind('click',function(e){ var $this = $(this) ... 
- jQuery 对文档的操作
			通过jquery方式实现页面各种节点的追加.修改.删除.复制等操作 节点追加 1 父子关系追加 /*************************************************** ... 
