程式碼隨想錄演算法訓練營day18 |530.二叉搜尋樹的最小絕對差 501.二叉搜尋樹中的眾數 236. 二叉樹的最近公共祖先

Tristan241001發表於2024-10-18

學習資料:https://programmercarl.com/0530.二叉搜尋樹的最小絕對差.html

530.二叉搜尋樹的最小絕對差(雙指標法,pre&cur,設定最小差值初始為無窮大,當差值<最小差值就更新最小差值)

點選檢視程式碼
# 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.result = float('inf')  # 設定最小值的初值為無窮大
        self.pre = None
    def getMinimumDifference(self, root):
        """
        :type root: TreeNode
        :rtype: int
        """
        if root is None:
            return
        self.getMinimumDifference(root.left)
        # 因為中序遍歷,這裡root.val肯定大於self.pre.val
        if self.pre is not None:
            self.result = min(self.result, root.val-self.pre.val) 
        self.pre = root
        self.getMinimumDifference(root.right)
        return self.result

        

501.二叉搜尋樹中的眾數(加兩個函式,init & searchBST;雙指標,當pre.val==cur.val,count+=1)

點選檢視程式碼
# 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.result = []
        self.pre = None
        self.count = 0
        self.maxCount = 0
    
    def searchBST(self, cur):
        if cur is None:
            return
        
        self.searchBST(cur.left)
        if self.pre is None:
            self.count = 1
        elif self.pre.val == cur.val:
            self.count += 1
        else:
            self.count = 1
        self.pre = cur

        if self.count == self.maxCount:
            self.result.append(cur.val)
        if self.count > self.maxCount:
            self.maxCount = self.count
            self.result = [cur.val]
        self.searchBST(cur.right)
        return

    def findMode(self, root):
        """
        :type root: TreeNode
        :rtype: List[int]
        """
        self.count = 0
        self.maxCount = 0
        self.pre = None
        self.result = []

        self.searchBST(root)
        return self.result
        
        

236.二叉樹的最近公共祖先(看程式碼簡單,但是想不通;遞迴;左右中:後序遍歷)

點選檢視程式碼
# Definition for a binary tree node.
# class TreeNode(object):
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None

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

PS:今天的後兩道題沒看懂,先抄一下
延遲了一天,因為昨晚有今年最大的月亮又恰逢難得的晴天,看到了月球超級靚但是還是差點意思沒看到凹,不過第一次看到土星,好遠好小好棒,還有環,就是沒注意顏色
昨天還吃了美味壽司,一個人幹了兩大碟,有實力兒
破公司的秋招,把人當猴耍

相關文章