-
LeetCode : 235. Lowest Common Ancestor of a Binary Search Tree🐳Dev/LeetCode 2023. 5. 5. 15:36
문제
Given a binary search tree (BST), find the lowest common ancestor (LCA) node of two given nodes in the BST.
According to the definition of LCA on Wikipedia: “The lowest common ancestor is defined between two nodes p and q as the lowest node in T that has both p and q as descendants (where we allow a node to be a descendant of itself).”
Constraints:
- The number of nodes in the tree is in the range [2, 105].
- 109 <= Node.val <= 109
- All Node.val are unique.
- p != q
- p and q will exist in the BST.
풀이
- 두 노드가 갈라지는 지점을 찾는 문제
- 이분 탐색을 사용하되, 둘을 동시에 만족하지 않는 노드(트리)를 반환
'🐳Dev > LeetCode' 카테고리의 다른 글
LeetCode : 1484. Group Sold Products By The Date (0) 2023.05.13 LeetCode : 200. Number of Islands (0) 2023.05.08 LeetCode : 98. Validate Binary Search Tree (0) 2023.05.03 LeetCode : 102. Binary Tree Level Order Traversal (0) 2023.05.01 LeetCode : 142. Linked List Cycle II (0) 2023.04.26