/
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* struct TreeNode left;
* struct TreeNode right;
* };
*/
bool isAncestor(struct TreeNode root, struct TreeNode p){
if(root == NULL)return false;
if(root == p)return true;
return isAncestor(root->left,p) || isAncestor(root->right,p);
}
struct TreeNode lowestCommonAncestor(struct TreeNode root, struct TreeNode p, struct TreeNode q) {
struct TreeNode low = root;
struct TreeNode queue[500];
int front=0,rear;
queue[0]=root;rear = 1;
while(front < rear){
struct TreeNode* tmp = queue[front];
if(tmp->left != NULL)queue[rear]=tmp->left;
if(tmp->right != NULL)queue[rear]=tmp->right;
}
for(int i = 0;i < rear;i){
if(isAncestor(queue[i],p) && isAncestor(queue[i],q)) low = queue[i];
}
return low;
}