🐳Dev/LeetCode

LeetCode : 235. Lowest Common Ancestor of a Binary Search Tree

fortune.00 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.

 

풀이

  • 두 노드가 갈라지는 지점을 찾는 문제
  • 이분 탐색을 사용하되, 둘을 동시에 만족하지 않는 노드(트리)를 반환