1
+ #include < iostream>
2
+ using namespace std ;
3
+
4
+ class Node {
5
+ public:
6
+ int data;
7
+ Node *left, *right;
8
+ Node (int x){
9
+ this ->data = x;
10
+ this ->left = this ->right = NULL ;
11
+ }
12
+ };
13
+
14
+
15
+ pair<int ,int > minTime (Node* node, int *result){
16
+ if (node->left == NULL && node->right == NULL )
17
+ return {0 ,1 };
18
+ cout << " Node " << node->data << endl;
19
+ pair<int ,int > ansl={0 ,-1 },ansr={0 ,-1 };
20
+ if (node->left )
21
+ ansl = minTime (node->left ,result);
22
+ if (node->right )
23
+ ansr = minTime (node->right ,result);
24
+ if (ansl.second == -1 )
25
+ return {ansr.first +1 ,ansr.second +1 };
26
+ if (ansr.second == -1 )
27
+ return {ansl.first +1 ,ansl.second +1 };
28
+ if (ansl.first <ansr.first ){
29
+ if (*result<ansl.first +1 +ansr.second )
30
+ *result = ansl.first +1 +ansr.second ;
31
+ return {ansl.first +1 , ansr.second +1 };
32
+ }
33
+ else {
34
+ if (*result<ansr.first +1 +ansl.second )
35
+ *result = ansr.first +1 +ansl.second ;
36
+ return {ansr.first +1 , ansl.second +1 };
37
+ }
38
+
39
+ }
40
+
41
+ int main ()
42
+ {
43
+ Node *root = new Node (0 );
44
+ root->left = new Node (1 );
45
+ root->right = new Node (2 );
46
+ root->left ->left = new Node (3 );
47
+ root->left ->right = new Node (4 );
48
+ root->right ->left = new Node (5 );
49
+ root->right ->right = new Node (6 );
50
+ root->left ->left ->left = new Node (10 );
51
+ root->left ->right ->left = new Node (9 );
52
+ root->right ->right ->left = new Node (7 );
53
+ root->right ->right ->right = new Node (8 );
54
+
55
+ cout << " Exec Start" << endl;
56
+ int *result = new int ;
57
+ minTime (root,result);
58
+ cout << *result << endl;
59
+
60
+ return 0 ;
61
+ }
0 commit comments