zoukankan      html  css  js  c++  java
  • 递归与二叉树_leetcode235

    # Definition for a binary tree node.
    # class TreeNode(object):
    # def __init__(self, x):
    # self.val = x
    # self.left = None
    # self.right = None


    # 二叉搜索树的公共祖先一定在[p,q]之间
    class Solution(object):
    def lowestCommonAncestor(self, root, p, q):
    """
    :type root: TreeNode
    :type p: TreeNode
    :type q: TreeNode
    :rtype: TreeNode
    """

    if not root:
    return root

    if p.val < root.val and q.val < root.val:
    return self.lowestCommonAncestor(root.left,p,q)
    if p.val > root.val and q.val > root.val:
    return self.lowestCommonAncestor(root.right,p,q)
    return root

  • 相关阅读:
    js 笔记
    openstack笔记
    Nginx
    Nginx
    Nginx
    nginx 服务器篇
    Nginx 原理篇
    MySQL 视图、触发器、函数、存储过程
    day41
    MySQL 作业题及答案
  • 原文地址:https://www.cnblogs.com/lux-ace/p/10546801.html
Copyright © 2011-2022 走看看