235. Lowest Common Ancestor of a Binary Search Tree
https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-search-tree/description/
Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BST.
According to the definition of LCA on Wikipedia: “The lowest common ancestor is defined between two nodes v and w as the lowest node in T that has both v and w as descendants (where we allow a node to be a descendant of itself).”
Thoughts
二叉搜索树上任意两个结点找最底层的共同祖宗结点。利用BST性质当两个结点都小于root时往左树遍历,都大于时往右树遍历,否则当前结点即答案。
Code
Analysis
时间复杂度O(lgN).
Last updated
Was this helpful?