Skip to content

Latest commit

 

History

History
72 lines (53 loc) · 1.83 KB

_236. Lowest Common Ancestor of a Binary Tree.md

File metadata and controls

72 lines (53 loc) · 1.83 KB

All prompts are owned by LeetCode. To view the prompt, click the title link above.

Back to top


First completed : July 15, 2024

Last updated : July 15, 2024


Related Topics : Tree, Depth-First Search, Binary Tree

Acceptance Rate : 65.77 %


Solutions

Python

# Definition for a binary tree node.
# 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':
        path = [root]
        pPath = []
        qPath = []

        def dfs(path: List['TreeNode'], qPath: List['TreeNode'], pPath: List['TreeNode']) -> None :
            if qPath and pPath :
                return
            
            if path[-1] == p :
                pPath.extend(path)
            if path[-1] == q :
                qPath.extend(path)

            if path[-1].left :
                path.append(path[-1].left)
                dfs(path, qPath, pPath)
                path.pop()
            if path[-1].right :
                path.append(path[-1].right)
                dfs(path, qPath, pPath)
                path.pop()
        
        dfs(path, qPath, pPath)

        while len(qPath) < len(pPath) :
            pPath.pop()
        
        while len(qPath) > len(pPath) :
            qPath.pop()

        while qPath[-1] != pPath[-1] :
            qPath.pop()
            pPath.pop()

        return pPath[-1]