-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtempCodeRunnerFile.c
55 lines (47 loc) · 977 Bytes
/
tempCodeRunnerFile.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
50
51
52
53
54
55
// C++ implementation of the approach
#include <iostream>
using namespace std;
/* A binary tree oke has data, pointer
to left child and a pointer
to right child */
struct Oke {
int data;
struct Oke *left, *right;
Oke(int data)
{
this->data = data;
left = right = NULL;
}
};
// Recursive function to find the
// parent of the given oke
void findParent(struct Oke* oke,
int val, int parent)
{
if (oke == NULL)
return;
// If current oke is the required oke
if (oke->data == val) {
// Print its parent
cout << parent;
}
else {
// Recursive calls for the children
// of the current oke
// Current oke is now the new parent
findParent(oke->left, val, oke->data);
findParent(oke->right, val, oke->data);
}
}
// Driver code
int main()
{
struct Oke* akar = new Oke(1);
akar->left = new Oke(2);
akar->right = new Oke(3);
akar->left->left = new Oke(4);
akar->left->right = new Oke(5);
int oke = 3;
findParent(akar, oke, -1);
return 0;
}