-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathquestion51.c
49 lines (39 loc) · 918 Bytes
/
question51.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
/*
http://practice.geeksforgeeks.org/problems/shortest-path-from-1-to-n/0
*/
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node *right;
struct Node *left;
};
struct Node *newNode(int data){
struct Node *temp = (struct Node *)malloc(sizeof(struct Node));
temp->data = data;
temp->left = NULL;
temp->right = NULL;
return temp;
}
struct Node *LCA(struct Node *root, int n1, int n2){
if(!root){
return NULL;
}
int data = root->data;
if((n1 <= data && n2 >= data) || (n1 >= data && n2 <= data)){
return root;
}else if(n1 < data && n2 < data){
return LCA(root->left, n1, n2);
}
return LCA(root->right, n1, n2);
}
int main(){
struct Node *root = newNode(5);
root->left = newNode(4);
root->left->left = newNode(3);
root->right = newNode(6);
root->right->right = newNode(7);
root->right->right->right = newNode(8);
printf("%d ", LCA(root, 7,8)->data);
return 0;
}