1
+ // { Driver Code Starts
2
+ #include < bits/stdc++.h>
3
+ using namespace std ;
4
+
5
+ // Tree Node
6
+ struct Node
7
+ {
8
+ int data;
9
+ Node* left;
10
+ Node* right;
11
+ Node (){
12
+ data = 0 ;
13
+ left = right = NULL ;
14
+ }
15
+ Node (int x){
16
+ data = x;
17
+ left = right = NULL ;
18
+ }
19
+ };
20
+
21
+ // } Driver Code Ends
22
+ /* A binary tree node
23
+
24
+ struct Node
25
+ {
26
+ int data;
27
+ struct Node* left;
28
+ struct Node* right;
29
+
30
+ Node(int x){
31
+ data = x;
32
+ left = right = NULL;
33
+ }
34
+ };
35
+ */
36
+
37
+ class Solution
38
+ {
39
+ public:
40
+ // Function to return the lowest common ancestor in a Binary Tree.
41
+ Node* lca (Node* root ,int n1 ,int n2 )
42
+ {
43
+ if (!root)
44
+ return NULL ;
45
+
46
+ if (root->data == n1 || root->data == n2)
47
+ return root;
48
+
49
+ Node* leftAns = lca (root->left , n1, n2);
50
+ Node* rightAns = lca (root->right , n1, n2);
51
+
52
+ // Now 3 important cases
53
+ if (leftAns && rightAns)
54
+ return root;
55
+
56
+ if (leftAns && !rightAns)
57
+ return leftAns;
58
+
59
+ if (!leftAns && rightAns)
60
+ return rightAns;
61
+
62
+ else
63
+ return NULL ;
64
+ }
65
+ };
66
+
67
+ // { Driver Code Starts.
68
+
69
+ Node* newNode (int val)
70
+ {
71
+ Node* temp = new Node;
72
+ temp->data = val;
73
+ temp->left = NULL ;
74
+ temp->right = NULL ;
75
+
76
+ return temp;
77
+ }
78
+
79
+
80
+ // Function to Build Tree
81
+ Node* buildTree (string str)
82
+ {
83
+ // Corner Case
84
+ if (str.length () == 0 || str[0 ] == ' N' )
85
+ return NULL ;
86
+
87
+ // Creating vector of strings from input
88
+ // string after spliting by space
89
+ vector<string> ip;
90
+
91
+ istringstream iss (str);
92
+ for (string str; iss >> str; )
93
+ ip.push_back (str);
94
+
95
+ // for(string i:ip)
96
+ // cout<<i<<" ";
97
+ // cout<<endl;
98
+ // Create the root of the tree
99
+ Node* root = newNode (stoi (ip[0 ]));
100
+
101
+ // Push the root to the queue
102
+ queue<Node*> queue;
103
+ queue.push (root);
104
+
105
+ // Starting from the second element
106
+ int i = 1 ;
107
+ while (!queue.empty () && i < ip.size ()) {
108
+
109
+ // Get and remove the front of the queue
110
+ Node* currNode = queue.front ();
111
+ queue.pop ();
112
+
113
+ // Get the current node's value from the string
114
+ string currVal = ip[i];
115
+
116
+ // If the left child is not null
117
+ if (currVal != " N" ) {
118
+
119
+ // Create the left child for the current node
120
+ currNode->left = newNode (stoi (currVal));
121
+
122
+ // Push it to the queue
123
+ queue.push (currNode->left );
124
+ }
125
+
126
+ // For the right child
127
+ i++;
128
+ if (i >= ip.size ())
129
+ break ;
130
+ currVal = ip[i];
131
+
132
+ // If the right child is not null
133
+ if (currVal != " N" ) {
134
+
135
+ // Create the right child for the current node
136
+ currNode->right = newNode (stoi (currVal));
137
+
138
+ // Push it to the queue
139
+ queue.push (currNode->right );
140
+ }
141
+ i++;
142
+ }
143
+
144
+ return root;
145
+ }
146
+
147
+ // Function for Inorder Traversal
148
+ void printInorder (Node* root)
149
+ {
150
+ if (!root)
151
+ return ;
152
+
153
+ printInorder (root->left );
154
+ cout<<root->data <<" " ;
155
+ printInorder (root->right );
156
+ }
157
+
158
+ int main () {
159
+ int t;
160
+ scanf (" %d" ,&t);
161
+ while (t--)
162
+ {
163
+ int a,b;
164
+ scanf (" %d %d " ,&a,&b);
165
+ string s;
166
+ getline (cin,s);
167
+ Node* root = buildTree (s);
168
+ Solution ob;
169
+ cout<<ob.lca (root,a,b)->data <<endl;
170
+ }
171
+ return 0 ;
172
+ }
173
+
174
+ // } Driver Code Ends
0 commit comments