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 ≤ 二叉搜索树元 ...
随机推荐
- 使用 Audacity 录制声卡声音
在Linux中使用 Audacity 录制电脑播放的声音非常简单,其实主要设置不在 Audacity 上,而是要设置好输入设备并选择对录音输入源. 首先确认输入设备中 内置音频的Monitor 没有被 ...
- 列举 contentType: 内容类型(MIME 类型)
常用的: 1.".doc"="application/msword" 2.".pdf"="application/pdf" ...
- InnoDB global status
常见参数 Innodb_buffer_pool_pages_free 发现 Innodb_buffer_pool_pages_free 为0 ,则说明buffer_pool 已经被用光,需要增大 in ...
- 视频专家之路【三】:Vs开发环境的搭建
本文是听了雷宵骅大神的课之后的总结,部分内容借用了其PPT的内容,如有侵权请告知删除. 雷宵骅大神的博客为:https://blog.csdn.net/leixiaohua1020 这里提及一点,原来 ...
- nodejs模块——fs模块 使用fs.write读文件
fs.write() fs.read(fd,buffer,offset,length[,position],callback(err,bytesWritten,buffer))接收6个参数. 参数说明 ...
- bcpow — 任意精度数字的乘方
bcpow — 任意精度数字的乘方 说明 string bcpow ( string $left_operand , string $right_operand [, int $scale ] ) 左 ...
- 实战:基于 Spring 的应用配置如何迁移至阿里云应用配置管理 ACM
最近遇到一些开发者朋友,准备将原有的Java Spring的应用配置迁移到 阿里云应用配置管理 ACM 中.迁移过程中,遇到不少有趣的问题.本文将通过一个简单的样例来还原迁移过程中遇到的问题和相关解决 ...
- 暴力——cf557c
//枚举高度[1,100000],>l的全部割掉,<l的砍掉最小的 #include<bits/stdc++.h> using namespace std; #define N ...
- Android API Levels 详解
Android API Levels 当你开发你的Android应用程序时,了解该平台API变更管理的基本方法和概念是很有帮助的.同样的,知道API级别标识以及该标识如何保障你的应用与实际硬件设备相兼 ...
- JS闭包的详解
目录 一.什么是闭包? 二.闭包有什么好处?应用在哪? 2.1 好处: 2.2 用法: 三.闭包需要注意的地方? 3.1 IE下会引发内存泄露 一.什么是闭包? 特点: 1 函数嵌套函数 2 内部函数 ...