# Any solution of Cpp within 36mm?

• I just wonder how to accomplish it in 36ms
since I saw it in Accepted Solutions Runtime Distributionj

which means is there any solution faster than this?

``````class Solution {
public:
TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {
int minval = min(p->val,q->val),maxval = max(p->val,q->val);
if(root->val>maxval)
return lowestCommonAncestor(root->left,p,q);
if(root->val<minval)
return lowestCommonAncestor(root->right,p,q);
return root;
}
};``````

• I'm pretty sure it involves luck. I got this one accepted in 36 ms after a few attempts:

``````TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {
int v = min(p->val, q->val), V = max(p->val, q->val);
while (true) {
if (root->val > V)
root = root->left;
else if (root->val < v)
root = root->right;
else
return root;
}
}
``````

Edit: Got 36 ms again after eleven more attempts.

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.