题目描述:

方法一:

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- 二叉搜索树中的众数的更多相关文章

  1. Java实现 LeetCode 501 二叉搜索树中的众数

    501. 二叉搜索树中的众数 给定一个有相同值的二叉搜索树(BST),找出 BST 中的所有众数(出现频率最高的元素). 假定 BST 有如下定义: 结点左子树中所含结点的值小于等于当前结点的值 结点 ...

  2. [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 ...

  3. Leetcode501.Find Mode in Binary Search Tree二叉搜索树中的众数

    给定一个有相同值的二叉搜索树(BST),找出 BST 中的所有众数(出现频率最高的元素). 假定 BST 有如下定义: 结点左子树中所含结点的值小于等于当前结点的值 结点右子树中所含结点的值大于等于当 ...

  4. LeetCode501.二叉搜索树中的众数

    题目,本题未做出,还有很多要学习 class Solution { public: vector<int>ans; int base,count,maxCount; void update ...

  5. [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 ...

  6. [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 ...

  7. [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 ...

  8. [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 ...

  9. 230. 二叉搜索树中第K小的元素

    230. 二叉搜索树中第K小的元素 题意 给定一个二叉搜索树,编写一个函数 kthSmallest 来查找其中第 k 个最小的元素. 你可以假设 k 总是有效的,1 ≤ k ≤ 二叉搜索树元素个数. ...

  10. leetcode 二叉搜索树中第K小的元素 python

          二叉搜索树中第K小的元素     给定一个二叉搜索树,编写一个函数 kthSmallest 来查找其中第 k 个最小的元素. 说明:你可以假设 k 总是有效的,1 ≤ k ≤ 二叉搜索树元 ...

随机推荐

  1. windows server 2016 支持多用户远程登录

    服务器设置多用户同时远程桌面,可以提高访问效率,避免人多抢登服务器. 1. 首先需要先安装远程桌面服务  配置组策略,运行框输入gpedit.msc,打开计算机配置–>管理模板—>wind ...

  2. Opencv 特征提取与检测-Haar特征

    Haar特征介绍(Haar Like Features) 高类间变异性 低类内变异性 局部强度差 不同尺度 计算效率高 这些所谓的特征不就是一堆堆带条纹的矩形么,到底是干什么用的?我这样给出 ...

  3. 【CSS】div的背景图完整图片覆盖

    最初的代码: .container_first { width: 100%; height: 100%; background: url(10176581.jpg); background-size: ...

  4. Java对象什么时候可以被垃圾回收?JVM的永久代中会发生垃圾回收么?

    当对象对当前使用这个对象的应用程序变得不可触及的时候,这个对象就可以被回收了.垃圾回收不会发生在永久代,如果永久代满了或者是超过了临界值,会触发完全垃圾回收(Full GC).如果你仔细查看垃圾收集器 ...

  5. docker安装cboard

    参考链接: https://hub.docker.com/r/chenlmdocker/docker-cboard/dockerfile https://www.cnblogs.com/zimo-ji ...

  6. 微信小程序学习笔记(一)--创建微信小程序

    一.创建小程序 1.申请帐号.安装及创建小程序,请参照官方文档里面的操作 https://developers.weixin.qq.com/miniprogram/dev/. 小程序在创建的时候会要求 ...

  7. 使用ionic来build安卓apk时,报CordovaError: Requirements check failed for JDK 1.8 or greater

    本地配置了JDK和jre的本地环境变量之后,在命令行中运行Java.javac等都能正常输出,但是在使用ionic cordova build android 来打包ionic的程序时,会提示 Cor ...

  8. hdu2089数位DP

    旁听途说这个名字很久了,了解了一下. 改题目的意思是给你若干区间,让你找寻区间内不含62或4的数. 首先暴力必然T...那么实际上就是说,想办法做一种预处理,在每次输入的时候取值运算就可以了. 既然是 ...

  9. java Logger 类

    package org.rx.common; import org.slf4j.LoggerFactory; import java.util.Collections; import java.uti ...

  10. CSS:目录

    ylbtech-CSS:目录 1.返回顶部 1. http://www.runoob.com/css/css-tutorial.html 2. https://www.w3school.com.cn/ ...