13 lines concise c++ answer


  • -3
    9
        class Solution {
    public:
        TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {
            if(!root||!p||!q) return nullptr;
            if(root->val < p->val && root->val < q->val) 
            	return lowestCommonAncestor(root->right,p,q);
            if(root->val > p->val && root->val > q->val) 
            	return lowestCommonAncestor(root->left,p,q);
    
            else
            	return root;
        }
    };

  • 0
    S

    It's not necessarily a BST, so your method will not work.


  • 0
    9

    yes , this is the answer of lca of a BST ,
    I POSTED IT ON A WRONG PLACE


Log in to reply
 

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