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