Python | Leetcode Python题解之第235题二叉搜索树的最近公共祖先

题目:

题解:

class Solution:
    def lowestCommonAncestor(self, root: TreeNode, p: TreeNode, q: TreeNode) -> TreeNode:
        ancestor = root
        while True:
            if p.val < ancestor.val and q.val < ancestor.val:
                ancestor = ancestor.left
            elif p.val > ancestor.val and q.val > ancestor.val:
                ancestor = ancestor.right
            else:
                break
        return ancestor

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-07-15 08:00:03       67 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-15 08:00:03       72 阅读
  3. 在Django里面运行非项目文件

    2024-07-15 08:00:03       58 阅读
  4. Python语言-面向对象

    2024-07-15 08:00:03       69 阅读

热门阅读

  1. 简单理解Lua 协程(coroutine)

    2024-07-15 08:00:03       19 阅读
  2. DangerWind-RPC-framework---七、序列化算法

    2024-07-15 08:00:03       22 阅读
  3. Android RecyclerView

    2024-07-15 08:00:03       18 阅读
  4. 在Ubuntu 14.04上安装和保护phpMyAdmin的方法

    2024-07-15 08:00:03       23 阅读
  5. [NeetCode 150] Redundant Connection

    2024-07-15 08:00:03       26 阅读
  6. PyTorch使用细节

    2024-07-15 08:00:03       23 阅读
  7. Matplotlib库学习之figure.add_subplot函数

    2024-07-15 08:00:03       25 阅读
  8. uniapp 初始学习1

    2024-07-15 08:00:03       31 阅读
  9. 在 YAML 中的变量(使用 &和 * 定义及引用变量)

    2024-07-15 08:00:03       25 阅读
  10. Julia 交互式命令

    2024-07-15 08:00:03       25 阅读