Zero to Hero
article thumbnail
 

Lowest Common Ancestor of a Binary Tree - LeetCode

Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.

leetcode.com

1. DFS

class Solution:
    def lowestCommonAncestor(self, root: TreeNode, p: TreeNode, q: TreeNode) -> 'TreeNode':
        p_seq, q_seq = [], []

        def dfs(cur, seq):
            nonlocal p_seq, q_seq
            if not cur:
                return
            seq.append(cur)
            if not p_seq and cur.val == p.val:
                p_seq = seq[::]
            elif not q_seq and cur.val == q.val:
                q_seq = seq[::]

            dfs(cur.left, seq)
            dfs(cur.right, seq)
            seq.pop()

        dfs(root, [])
        q_seq_set = set(q_seq)
        while p_seq:
            cur_node = p_seq.pop()
            if cur_node in q_seq_set:
                return cur_node

DFS 탐색을 통해서 타깃이 되는 2개의 노드의 경로를 담는다.

담은 뒤에 두 경로를 뒤에서부터 비교하면서 가장 먼저 매칭 되는 노드가 공통 조상이고 이것을 반환한다.

노드의 값이 아니라 노드 자체를 반환해야 해서 담아서 관리하는 방법을 사용했다.

2. DFS(약간 최적화)

class Solution:
    def lowestCommonAncestor(self, root: TreeNode, p: TreeNode, q: TreeNode) -> 'TreeNode':
        p_seq, q_seq = [], []

        def dfs(cur, seq):
            nonlocal p_seq, q_seq
            if not cur:
                return
            seq.append(cur)
            if not p_seq and cur.val == p.val:
                p_seq = seq[::]
            elif not q_seq and cur.val == q.val:
                q_seq = seq[::]
            if p_seq and q_seq:
                return

            dfs(cur.left, seq)
            dfs(cur.right, seq)
            seq.pop()

        dfs(root, [])
        q_seq_set = set(q_seq)
        while p_seq:
            cur_node = p_seq.pop()
            if cur_node in q_seq_set:
                return cur_node

공통 조상을 찾는 부분을 list 순회가 아니라 set 순회로 바꿔서 시간을 단축시켰다.

또 타깃이 되는 2개의 노드의 탐색이 끝났다면 더 이상 탐색하지 않아도 되기 때문에 중간에 리턴 시켰다.

 

 

'Algorithm' 카테고리의 다른 글

240. Search a 2D Matrix II  (0) 2021.05.18
279. Perfect Squares  (0) 2021.05.17
200. Number of Islands  (0) 2021.05.17
75. Sort Colors  (0) 2021.05.16
337. House Robber III  (0) 2021.05.16
profile

Zero to Hero

@Doljae

포스팅이 좋았다면 "좋아요❤️" 또는 "구독👍🏻" 해주세요!