Running time problem. I know there something wrong for the thrid if statement, but I can't figure out why


  • 0
    O
    class Solution {
    public:
        TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {
            while(root!=NULL){
                if(root->val<p->val&&root->val<q->val) root=root->right;
                if(root->val>p->val&&root->val>q->val) root=root->left;
                if(root->val>(p->val<q->val?p->val:q->val)&&root->val<(p->val>q->val?p->val:q->val))
               break;
            }
            return root;
        }
    };

Log in to reply
 

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