My code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
class TreeNode:
    def __init__(self, x):
        self.val = x
        self.left = None
        self.right = None

class Solution:
    def lowestCommonAncestor(self, root: 'TreeNode', p: 'TreeNode', q: 'TreeNode') -> 'TreeNode':
        if root == None:
            return None

        l = Solution.lowestCommonAncestor(self,root.left, p, q)
        r = Solution.lowestCommonAncestor(self,root.right, p, q)    

        if root == p or root == q:
            return root
        elif l and r:
            return root
        return l or r