• 剑指 Offer 68





    方法一:递归

    class Solution(object):
        # 递归思路:
        #   当p,q都在root的右子树中时,递归root.right并返回;
        #   当p,q都在root的左子树中时,递归root.left并返回;
        #   返回值:最近公共祖先root。
        def lowestCommonAncestor(self, root, p, q):
            """
            :type root: TreeNode
            :type p: TreeNode
            :type q: TreeNode
            :rtype: TreeNode
            """
            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
    

    方法二:迭代

    class Solution(object):
        # 迭代思路:
        # 先特判:
        #   当节点root为空时:return None;
        #   当p、q值域相同时,return p 或 return q;
        # 再遍历:
        #   如果,当p,q都在root的右子树中,则遍历至root.right:root = root.right;
        #   否则,当p,q都在root的左子树中,则遍历至root.left:root = root.left;
        #   否则,说明找到了最近公共祖先,则return root。
        def lowestCommonAncestor(self, root, p, q):
            """
            :type root: TreeNode
            :type p: TreeNode
            :type q: TreeNode
            :rtype: TreeNode
            """
            if not root:
                return None
            if p.val == q.val:
                return p
            while root:
                if p.val > root.val and q.val > root.val:
                    root = root.right
                elif p.val < root.val and q.val < root.val:
                    root = root.left
                else:
                    return root
    

    针对一般二叉树

    class Solution(object):
       # 针对一般二叉树。
        # 先特判:
        #   当节点root为空时:return None;
        #   当p或q的值域等于root时,return root;
        # 再递归判断root的左、右子树;
        #   若左右子树都不为空:return root;
        #   若左真右假:return 左;
        #   若左假右真:return 右;
        #   否则,左右都为空:return None;
        def lowestCommonAncestor(self, root, p, q):
            """
            :type root: TreeNode
            :type p: TreeNode
            :type q: TreeNode
            :rtype: TreeNode
            """
            if not root:
                return None
            # if p.val == q.val:
            #     return p
            if p.val == root.val or q.val == root.val:
                return root
            left = self.lowestCommonAncestor(root.left, p, q)
            right = self.lowestCommonAncestor(root.right, p, q)
            if left and right:
                return root
            elif left:
                return left
            elif right:
                return right
            else:
                return None
    
  • 相关阅读:
    kafka学习3——单机伪集群(window版)
    (转)在阿里云 CentOS 服务器(ECS)上搭建 nginx + mysql + php-fpm 环境
    渗透测试工具sqlmap基础教程
    编译安装nginx后service nginx start 启动不了
    centos6.5上tomcat的安装
    centos6.5上安装淘宝tfs系统
    解决nagios登录不了的问题
    centos6.5上网络不通解决方法
    redis远程密码连接
    centos7上定时将mysql数据导出并且发送到指定的邮箱
  • 原文地址:https://www.cnblogs.com/panweiwei/p/13585987.html
Copyright © 2020-2023  润新知