SHARE
    TWEET
    miglss

    42.YA

    Apr 12th, 2025
    442
    0
    Never
    Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
    Python 0.53 KB | None | 0 0
    1. class Solution:
    2. def lowestCommonAncestor(self, root: 'TreeNode', p: 'TreeNode', q: 'TreeNode') -> 'TreeNode':
    3. def dfs(root):
    4. if root in (None, p, q):
    5. return root
    6. left = dfs(root.left)
    7. right = dfs(root.right)
    8. if left and right:
    9. return root
    10. elif left:
    11. return left
    12. elif right:
    13. return right
    14. else:
    15. return None
    16. return dfs(root)
    Advertisement
    Add Comment
    Please, Sign In to add comment
    Public Pastes
    We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
    Not a member of Pastebin yet?
    Sign Up, it unlocks many cool features!

    AltStyle によって変換されたページ (->オリジナル) /