236-Lowest-Common-Ancestor-Of-A-Binary-Tree

Sat 17 May 2025

https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-tree

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
class Solution:
  def lowestCommonAncestor(self, root: 'TreeNode', p: 'TreeNode', q: 'TreeNode') -> 'TreeNode':
    if not root or root == p or root == q:
      return root

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

    if l and r:
      return root
    return l or r
new Solution().lowestCommonAncestor()

Score: 5

Category: leetcode