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