题解 | #二叉搜索树的最近公共祖先#
二叉搜索树的最近公共祖先
https://www.nowcoder.com/practice/d9820119321945f588ed6a26f0a6991f
因该是此题最好的写法了把
int lowestCommonAncestor(TreeNode* root, int p, int q) {
while(root)
{
if(root->val >p && root->val>q)
{
root=root->left;
}else if(root->val<p && root->val<q)
{
root=root->right;
} else
{
return root->val;
}
};
return 0;
}