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 ≤ 二叉搜索树元 ...
随机推荐
- stdin stdout stderr - 标准 I/O 流
Fd #include <stdio.h> Fd extern FILE *stdin; Fd extern FILE *stdout; Fd extern FILE *stderr; D ...
- 【LeetCode】Heap
[215] Kth Largest Element in an Array [Medium] 给个无序数组,返回第K大的数字. 方法1. 直接使用优先队列 priority_queue class S ...
- 浅谈JAVA线程
一.线程(Thread) 1.线程 线程:是指程序中的顺序流 多线程:一个程序中的多个顺序流同时执行 (1)线程的状态: 新生 就绪 运行 阻塞 终止 (2)学习多线程: 1)线程的创建 2)线程的状 ...
- Office VBA 参考
Office VBA 参考:https://docs.microsoft.com/zh-CN/office/vba/api/overview/
- renren-fast-vue-动态路由-添加路由-方式一(直接在原有结构上添加)
在原有文件夹夹下新建自己的组件 在 mock/modules/sys-menu.js 中引入 实现路由的添加
- JSP自定义标签(转)
1. TagSupport与BodyTagSupport的区别 TagSupport与BodyTagSupport的区别主要是标签处理类是否需要与标签体交互,如果不需要交互的就用TagSupport, ...
- CSS:目录
ylbtech-CSS:目录 1.返回顶部 1. http://www.runoob.com/css/css-tutorial.html 2. https://www.w3school.com.cn/ ...
- JDK的下载及安装
JDK下载及安装 JDK的下载 官网下载 点击进入之后,显示的是当前版本最新的,点击downloads,选择适合自己电脑的版本下载 下载历史版本要一直往下拉,找到如图: 点击之后会显示以往的版本 环境 ...
- mvn eclipse:eclipse
pom.xml 在哪个文件夹, 你就在哪里按shift 右键,,[在此处打开命令窗口] 执行那个命令. mvn eclipse:eclipse
- 正则化:L0 vs L1 vs L2
原文地址:https://www.jianshu.com/p/e5c9a9fc84d4 为什么正则化可以缓解过拟合? 过拟合时,拟合函数的系数往往非常大.过大的权重会导致模型过多地学习到某些数据的个性 ...