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 ≤ 二叉搜索树元 ...
随机推荐
- 2018-2-13-win10-uwp-网络编程
title author date CreateTime categories win10 uwp 网络编程 lindexi 2018-2-13 17:23:3 +0800 2018-2-13 17: ...
- AMS5601的ardunio和STM32驱动开发
AMS5601的ardunio和STM32驱动开发 本文有麦粒电子撰写,并提供相应产品服务. 前言 目前ams关于磁编码芯片用的比较多的可能是ams5600,能够输出pwm信号,电压信号以及I2C通信 ...
- POJ-1155 TELE 树形背包dp
dp[u][i]代表以u为根的子树选i个叶子的最大收益 那么dp[u][i]=max(dp[u][i],dp[v][k]+dp[u][i-k]-len) (1=<k<=i) 细节看代码: ...
- icomoon字体图标引用代码
1.第一步在样式里声明字体:告诉别人我们自己定义的字体. @font-face{ /*声明字体 引用字体*/ font-family:'icomoon'; src:url('fonts/icomoon ...
- Array Beauty
Array Beauty 给出一个长度为n的序列\(\{a_i\}\),定义一个序列的权值为其中元素两两之差的绝对值的最小值,询问\(\{a_i\}\)长度为K的子序列的权值之和\(\% 998244 ...
- cboard进行访问,汉化
- classmethod和staticmethod
假设有这么一个 class class Date(object): def __init__(self, day=0, month=0, year=0): self.day = day self.mo ...
- 【leetcode】977. Squares of a Sorted Array
题目如下: Given an array of integers A sorted in non-decreasing order, return an array of the squares of ...
- (转)OpenFire源码学习之十五:插件开发
转:http://blog.csdn.net/huwenfeng_2011/article/details/43418493 Plugin接口规范 插件是openfire功能的增强表现,它的主要任务: ...
- java基础集合底层介绍
ArrayList.Vector.HashMap.HashTable.HashSet的默认初始容量.加载因子.扩容增量 这里要讨论这些常用的默认初始容量和扩容的原因是: 当底层实现涉及到扩容时,容器或 ...