day16-数据结构力扣

530.二叉搜索树的最小绝对差

题目链接530. 二叉搜索树的最小绝对差 - 力扣(LeetCode)

思路

看到题,我想到的是,先中序遍历得到结果数组,因为二叉搜索树遍历得到的数组是有序的

我对前后元素求差值,存放到一个数组,然后再求这个数组的最小值

试一下

可以

提交

python 复制代码
# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution:
    def getMinimumDifference(self, root: Optional[TreeNode]) -> int:
        if not root:
            return 0
        res=self.traversal(root)
        print(res)
        minus=[]
        for i in range(len(res)-1):
            minus.append(res[i+1]-res[i])
        return min(minus)

        
    def traversal(self,root: Optional[TreeNode])->List:
        if not root:
            return []
        return self.traversal(root.left)+[root.val]+self.traversal(root.right)

501.二叉搜索树中的众数

题目链接 501. 二叉搜索树中的众数 - 力扣(LeetCode)

思路

其实比较简单的思路,时间复杂度可能更高,但是我现在只求能过,提交通过

我把二叉搜索树这种可以遍历得到数组,然后再进行处理的都这么解决

先遍历,得到数组,统计每个数出现的次数,返回出现次数最多的数。

但是这里有一个难点就是,需要把数字和出现的次数对应记录(字典),然后不是返回次数最多的次数,而是这个次数对应的数字

提交

python 复制代码
# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution:
    def findMode(self, root: Optional[TreeNode]) -> List[int]:
        if not root:
            return []
        nums=self.traversal(root)
        count={}
        for num in nums:
            count[num]=count.get(num,0)+1
        max_num=None
        max_cnt=0
        res=[]
        for num,cnt in count.items():
            if cnt>max_cnt:
                max_cnt=cnt
        for num,cnt in count.items():
            if max_cnt==cnt:
                res.append(num)
        return res


    def traversal(self,root):
        if not root:
            return []
        return self.traversal(root.left)+[root.val]+self.traversal(root.right)

236. 二叉树的最近公共祖先

题目链接 236. 二叉树的最近公共祖先 - 力扣(LeetCode)

思路

题的意思是大概能看懂,自己看图大概也知道是什么。

但是判断依据不知道怎么写。

p,q这两个节点从底往上遍历,交汇的最近的节点就是他们的最近公共祖先

从下往上(后序)遍历二叉树,分别在左右子树里找 p 和 q:

  • 如果左右都找到了,说明当前节点就是最近公共祖先;

  • 如果只在一边找到,就把那边的结果往上返回;

  • 遇到空节点、p 或 q 就直接返回。

提交

python 复制代码
# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None

class Solution:
    def lowestCommonAncestor(self, root: 'TreeNode', p: 'TreeNode', q: 'TreeNode') -> 'TreeNode':
        if root==q or root==p or root is None:
            return root

        left=self.lowestCommonAncestor(root.left,p,q)
        right=self.lowestCommonAncestor(root.right,p,q)

        if left is not None and right is not None:
            return root
        if left is None and right is not None:
            return right
        elif left is not None and right is None:
            return left
        else:
            return None
相关推荐
不会就选b几秒前
算法日常・每日刷题--<贪心>26
数据结构·算法
hetao17338372 小时前
2026-09-29 hetao1733837 的刷题记录
c++·算法
欣欣之王来了2 小时前
AI合规专项:AI算法透明度的合规要求
人工智能·算法
wzdark2 小时前
分块思想在算法优化中的实践与应用4
算法
Because_of_Her13 小时前
dfs-洪水填充技巧
笔记·算法·深度优先·洪水填充
小静AI工程实验室3 小时前
RSA 算法详解:从模幂原理到 OAEP 加密、PSS 签名与 Python 实验
python·算法
xzal123 小时前
GESP Python笔记
笔记·python·算法
wzdark3 小时前
算法中的记忆化思想与重复子问题优化4
算法
Discipline10293 小时前
流水线运行是否正随时间变得更加集中?
算法
Logic1013 小时前
C语言/数据结构位运算题解:异或XOR找出书柜中编号只出现一次的那一层——只出现一次的数字
c语言·数据结构·数组·位运算·时间复杂度·算法题·异或性质