[알고리즘] LCA(Lowest Common Ancestor) 최소공통조상
Binary Tree에서 최소공통조상 LCA를 찾는 로직. 직접 Tree와 스택을 종이에 써보면서 따라가보면 이해가 쉽다. Leetcode 236. Lowest Common Ancestor of a Binary Tree Input/Output Input: root = [3,5,1,6,2,0,8,null,null,7,4], p = 5, q = 1 Output: 3 Explanation: The LCA of nodes 5 and 1 is 3. Solution # Definition for a binary tree node. # class TreeNode: # def __init__(self, x): # self.val = x # self.left = None # self.right = None class S..
2022. 12. 2.