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

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

题目链接:LeetCode - The World's Leading Online Programming Learning Platform

题目链接/文章讲解:代码随想录

视频讲解:二叉搜索树中,需要掌握如何双指针遍历!| LeetCode:530.二叉搜索树的最小绝对差_哔哩哔哩_bilibili

解题思路:

因为是二叉搜索树 中序遍历 相邻的两个数的绝对值差中最小的就是答案。 

# Definition for a binary tree node.
# class TreeNode(object):
#     def __init__(self, val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution(object):
    def getMinimumDifference(self, root):
        """
        :type root: TreeNode
        :rtype: int
        """
        order=[]
        def traversal(root):
            if not root:
                return 
            traversal(root.left)
            order.append(root.val)
            traversal(root.right)
        traversal(root)
        res=float('inf')
        for i in range(len(order)-1):
            res=min(res,abs(order[i]-order[i+1]))
        return res

501.二叉搜索树中的众数 

题目链接:LeetCode - The World's Leading Online Programming Learning Platform

题目链接/文章讲解:代码随想录

视频讲解:不仅双指针,还有代码技巧可以惊艳到你! | LeetCode:501.二叉搜索树中的众数_哔哩哔哩_bilibili

解题思路:

双指针 还有中序遍历

先进左递归

中递归时 如果此时pre的值和cur的值一样那就增加count+1,如果不一样或者就重新设置count值为1 结束后更新pre值。然后更新res值 如果count=max_count, 放入result,如果>max_count, 重制result

再进入右递归。

返回值。 

# Definition for a binary tree node.
# class TreeNode(object):
#     def __init__(self, val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution(object):
    def __init__(self):
        self.pre = TreeNode(None)
        self.count = 0
        self.max_count = 0
        self.result = []
    def findMode(self, root):
        """
        :type root: TreeNode
        :rtype: List[int]
        """
        if not root:
            return None
        self.findMode(root.left)
        if self.pre.val==root.val:
            self.count += 1
        else:
            self.count = 1
        self.pre=root
        if self.count == self.max_count:
            self.result.append(root.val)
        
        if self.count > self.max_count:
            self.max_count = self.count
            self.result = [root.val]
        self.findMode(root.right)
        return self.result

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

题目链接:LeetCode - The World's Leading Online Programming Learning Platform

题目链接/文章讲解:代码随想录

视频讲解:自底向上查找,有点难度! | LeetCode:236. 二叉树的最近公共祖先_哔哩哔哩_bilibili

解题思路:

采用后序遍历 basic condition 为如果没有node或者当前node等于p/q 返回当前node,

先进行左右遍历 中时判断是否左右子树有node在 如果同时有 返回当前node

如果只有左子树有node返回 只需要继续返回做子树的node 注意这里已经包含了左右子树有node在的情况 反之 返回右节点

class Solution(object):
    def lowestCommonAncestor(self, root, p, q):
        """
        :type root: TreeNode
        :type p: TreeNode
        :type q: TreeNode
        :rtype: TreeNode
        """
        if not root 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
        elif right:
            return right

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值