题解 | #二叉搜索树的最近公共祖先#
二叉搜索树的最近公共祖先
https://www.nowcoder.com/practice/d9820119321945f588ed6a26f0a6991f
int lowestCommonAncestor(struct TreeNode* root, int p, int q ) {
// write code here
int low = 0, high = 0;
if (p > q) {
low = q;
high = p;
} else {
low = p;
high = q;
}
if (low <= root->val && high >= root->val) {
return root->val;
} else if (high < root->val) {
return lowestCommonAncestor(root->left, p, q);
} else {
return lowestCommonAncestor(root->right, p, q);
}
}