Re: [LeetCode] 刷到面试 Grind169 C++

楼主: SuiseiLeda (星街雷达)   2023-03-19 11:51:34
235. Lowest Common Ancestor of a Binary Search Tree easy题
越来越熟练囉
class Solution {
public:
TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {
if(root->val > p->val && root->val > q->val){
return lowestCommonAncestor(root->left, p, q);
}
else if (root->val < q->val && root->val < p->val){
return lowestCommonAncestor(root->right, p, q);
}
return root;
}
};
作者: DDFox (冒险者兼清洁工)   2023-03-19 11:52:00
大师
作者: DreaMaker167 (dreamaker)   2023-03-19 11:52:00
大师

Links booklink

Contact Us: admin [ a t ] ucptt.com