Accepted non-recursive c code


  • 0
    H
    struct TreeNode* lowestCommonAncestor(struct TreeNode* root, struct TreeNode* p, struct TreeNode* q) {
        struct TreeNode *lca = root;
        struct TreeNode *p_next, *q_next;;
        while(lca != NULL) {
            if(p->val > lca->val)
                p_next = lca->right;
            else if(p->val == lca->val)
                p_next = lca;
            else 
                p_next = lca->left;
            if(q->val > lca->val)
                q_next = lca->right;
            else if(q->val == lca->val)
                q_next = lca;
            else
                q_next = lca->left;
            if(p_next != q_next)
                return lca;
            lca = p_next;
        }
        return lca;
    }

Log in to reply
 

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