package
0.0.0-20241220224003-b7cf03a90b2b
Repository: https://github.com/szhou12/leetcode-go.git
Documentation: pkg.go.dev
# README
235. Lowest Common Ancestor of a Binary Search Tree
Solution idea
LCA
-
LCA 经典题型: 两个input nodes
p, q
一定存在于树中 -
利用 BST 性质: node X is LCA of p, q iff X's value is between p and q's values.
- 如果 current node
X
比p, q
都小, 说明 current nodeX
太小了, 往它的右子树--增大X
值的方向看看呢? - 如果 current node
X
比p, q
都大, 说明 current nodeX
太大了, 往它的左子树--缩小X
值的方向看看呢? - 否则, 看来current node
X
恰好夹在p, q
中间, 说明 current nodeX
正合适, 就它了
- 如果 current node