11-25 习题

蓝桥杯

8760.验题人的生日

def iszhishu(x):

    for i in range(2,x):
        if x%i==0:
            return 0

    return 1
for i in range(1,31):
  if i%2==0:
    y=iszhishu(i)
    if y==1:
      print(i)

【算法赛】蓝桥小课堂:

import os
import sys

# 请在此输入您的代码
a,b,c=map(int,input().split())
x=(a+b+c)/2
if a+b>c and a+c>b and b+c>a :
  s=x*(x-a)*(x-b)*(x-c)
  print(int(s))
else:
  print('-1')

144.反转二叉树


class Solution(object):
    def mirrorTree(self, root):
        """
        :type root: TreeNode
        :rtype: TreeNode
        """
        # 判断树是否为空
        if not root:
            return None
        # 当前节点的左右子树交换
        root.left,root.right=root.right,root.left
        # 到下一层
        self.mirrorTree(root.left)
        self.mirrorTree(root.right)
# `      最后返回根节点`
        return root

98. 验证二叉搜索树

# 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 isValidBST(self, root):
        """
        :type root: TreeNode
        :rtype: bool
        """
        def dfs(node,lower,upper):
            # 终止田间
            if not node:
                return  True
            # 处理当前层逻辑
            val =node.val
            # 判断是否为二叉搜索树
            if val<=lower or val>=upper:
                return False
            # 进入到下一层
            # 分治
            # 左子树
            if not dfs(node.left,lower,val):
                return False
            # 右子树
            if not dfs(node.right,val,upper):
                return False
            return True
        return dfs(root,float('-inf'),float('inf'))
        

 

你可能感兴趣的:(深度优先,算法)