zoukankan      html  css  js  c++  java
  • 剑指 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
    
  • 相关阅读:
    [Codeforces Round #617 (Div. 3)] 题解 A,B,C,D,E1,E2,F
    [Codeforces Round #611 (Div. 3)] C. Friends and Gifts (随机大法好)
    [Hello 2020] D. New Year and Conference (ST表,排序)
    [Hello 2020] C. New Year and Permutation (组合数学)
    Codeforces Beta Round #7 C. Line (扩展欧几里德)
    扩展欧几里德
    Codeforces Round #349 (Div. 2) D. World Tour (最短路)
    HDU 4052 Adding New Machine (线段树+离散化)
    HDU 3265 Posters (线段树+扫描线)(面积并)
    HDU 1828 Picture (线段树+扫描线)(周长并)
  • 原文地址:https://www.cnblogs.com/panweiwei/p/13585987.html
Copyright © 2011-2022 走看看