235. Lowest Common Ancestor of a Binary Search Tree
https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-search-tree/description/
Thoughts
Code
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {
auto cur = root;
while (cur != nullptr) {
if (p->val < cur->val && q->val < cur->val) {
cur = cur->left;
} else if (p->val > cur->val && q->val > cur->val) {
cur = cur->right;
} else return cur;
}
return cur;
}
};
Analysis
Last updated