Share my C++ 26ms recursion


  • 0
    class Solution {
    public:
        TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {
            int Min = min(p->val, q->val);
            int Max = max(p->val, q->val);
            if(root->val <= Max && root->val >= Min) return root;
            if(root->val > Max) return lowestCommonAncestor(root->left, p, q);
            return lowestCommonAncestor(root->right, p, q);
        }
    };
    

Log in to reply
 

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