代码随想录算法训练营第二十一天 | 530.二叉搜索树的最小绝对差 、501.二叉搜索树中的众数 、236. 二叉树的最近公共祖先

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

# 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:
        res = []
        r = float('inf')
        def buildalist(root):
            if not root: return None
            if root.left: buildalist(root.left)
            res.append(root.val)
            if root.right: buildalist(root.right)
            return res

        buildalist(root)
        for i in range(len(res)-1):
            r = min(abs(res[i] - res[i+1]),r)
        return r
        #双指针
        stack = []
        cur = root
        pre = None
        result = float('inf')

        while cur or stack:
            if cur:
                stack.append(cur)
                cur = cur.left
            else:
                cur = stack.pop()
                if pre:
                    result = min(result,abs(cur.val - pre.val))

                pre = cur 
                cur = cur.right

        return result

501.二叉搜索树中的众数

# 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]:
        def __init__(self):
            self.pre = TreeNode()
            self.count = 0
            self.max_count = 0
            self.result = []

        def findMode(self,root: TreeNode) -> List[int]:
            if not root: return None
            self.search_BST(root)
            return self.result

        def search_BST(self,cur: TreeNode) -> None:
            if not cur: return None
            self.search_BST(cur.left)

            if not self.pre:
                self.count = 1
            elif self.pre.val == cur.val:
                self.count += 1
            else:
                self.count = 1
            self.pre = cur

            if sel.count == self.max_count:
                self.max_count = self.count
                self.result = [cur.val]

            self.search_BST(cur.right)
class Solution:
    def findMode(self, root: TreeNode) -> List[int]:
        stack = []
        cur = root
        pre = None
        maxCount, count = 0, 0
        res = []
        while cur or stack:
            if cur:  # 指针来访问节点,访问到最底层
                stack.append(cur)
                cur = cur.left
            else:  # 逐一处理节点
                cur = stack.pop()
                if pre == None:  # 第一个节点
                    count = 1
                elif pre.val == cur.val:  # 与前一个节点数值相同
                    count += 1
                else:
                    count = 1
                if count == maxCount:
                    res.append(cur.val)
                if count > maxCount:
                    maxCount = count
                    res.clear()
                    res.append(cur.val)

                pre = cur
                cur = cur.right
        return res	

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

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

        if left and right:
            return root
        if left:
            return left
        else:
            return right

       力扣

  • 自己看到题目的第一想法

       毫无头绪            

  • 看完代码随想录之后的想法 

  • 自己实现过程中遇到哪些困难

  • 今日收获,记录一下自己的学习时长
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值