Given a binary search tree(BST), find the lowest common ancestor of two given nodes in the BST.
Node* LCA(Node* root, Node* p, Node* q) { if (!root || !p || !q) return NULL; if (max(p->data, q->data) < root->data) return LCA(root->left, p, q); else return LCA(root->right, p, q); else return root; }
时间: 2024-10-15 09:51:27