Question: Let us store a binary tree in an array (root at position 1). Assume that all the keys are positive, and 0 represents the NULL

 Let us store a binary tree in an array (root at

Let us store a binary tree in an array (root at position 1). Assume that all the keys are positive, and 0 represents the NULL node. Please write a function to find the Lowest Common Ancestor of two nodes at positions P1 and P2. Author Organization IT Format of function: Code Size 16 KB Limit int LCA( int [], int P1, int P2 ); Time Limit 400 ms where I is the tree. The function is supposed to return the position index of the lowest common ancestor of the nodes at P1 and P2 . If one of the nodes is NULL, you must print in a line ERROR: T[x] is NULL where x is the NULL position (if both p1 and P2 are NULL positions, then output P1 ), and return 0 as the signal of error. Memory Limit 64 MB Sample program of judge: #include #define MAXN 20 void BuildTree( int []); /* details omwitted / int LCA( int T[], int P1, int P2 ); int main() { int T[MAXN] int pi, p2, P; BuildTree T); scanf("%d %d", &pi, &p2); P = LCACT, p1, p2); if (P) printf("%d %d ", P, T[p]); return; /* Your function will be put here Sample Input 1 (for the tree shown in the Figure): 3 1 (10 0 0 20900 6 8 11 4 Sample Output 1: 23 Sample Input 2 (for the same tree in Sample 1): 13 10

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!