代码随想录day37|738. 单调递增的数字968. 监控二叉树

738. 单调递增的数字

class Solution:
    def monotoneIncreasingDigits(self, n: int) -> int:
        n=str(n)
        flag = len(n)
        for i in range(len(n)-1,0,-1):
            if n[i] 

968. 监控二叉树

# 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 minCameraCover(self, root: Optional[TreeNode]) -> int:
        self.result = 0
        if self.traversal(root) ==0:
            self.result +=1
        return self.result
    def traversal(self,cur):
        #0是无覆盖,1是有摄像头,2是有覆盖
        if cur == None:
            return 2
        left = self.traversal(cur.left)
        right = self.traversal(cur.right)
        if left == 2 and right==2:
            return 0
        if left == 0 or right == 0:
            self.result +=1
            return 1
        if left ==1 or right==1:
            return 2

你可能感兴趣的:(算法,leetcode,职场和发展)