|
| 1 | +//{ Driver Code Starts |
| 2 | +#include <bits/stdc++.h> |
| 3 | +using namespace std; |
| 4 | + |
| 5 | +struct Node |
| 6 | +{ |
| 7 | + int data; |
| 8 | + struct Node *left; |
| 9 | + struct Node *right; |
| 10 | +}; |
| 11 | +Node* newNode(int val) |
| 12 | +{ |
| 13 | + Node* temp = new Node; |
| 14 | + temp->data = val; |
| 15 | + temp->left = NULL; |
| 16 | + temp->right = NULL; |
| 17 | + |
| 18 | + return temp; |
| 19 | +} |
| 20 | +Node* buildTree(string str) |
| 21 | +{ |
| 22 | + // Corner Case |
| 23 | + if(str.length() == 0 || str[0] == 'N') |
| 24 | + return NULL; |
| 25 | + |
| 26 | + // Creating vector of strings from input |
| 27 | + // string after spliting by space |
| 28 | + vector<string> ip; |
| 29 | + |
| 30 | + istringstream iss(str); |
| 31 | + for(string str; iss >> str; ) |
| 32 | + ip.push_back(str); |
| 33 | + |
| 34 | + // Create the root of the tree |
| 35 | + Node* root = newNode(stoi(ip[0])); |
| 36 | + |
| 37 | + // Push the root to the queue |
| 38 | + queue<Node*> queue; |
| 39 | + queue.push(root); |
| 40 | + |
| 41 | + // Starting from the second element |
| 42 | + int i = 1; |
| 43 | + while(!queue.empty() && i < ip.size()) { |
| 44 | + |
| 45 | + // Get and remove the front of the queue |
| 46 | + Node* currNode = queue.front(); |
| 47 | + queue.pop(); |
| 48 | + |
| 49 | + // Get the current node's value from the string |
| 50 | + string currVal = ip[i]; |
| 51 | + |
| 52 | + // If the left child is not null |
| 53 | + if(currVal != "N") { |
| 54 | + |
| 55 | + // Create the left child for the current node |
| 56 | + currNode->left = newNode(stoi(currVal)); |
| 57 | + |
| 58 | + // Push it to the queue |
| 59 | + queue.push(currNode->left); |
| 60 | + } |
| 61 | + |
| 62 | + // For the right child |
| 63 | + i++; |
| 64 | + if(i >= ip.size()) |
| 65 | + break; |
| 66 | + currVal = ip[i]; |
| 67 | + |
| 68 | + // If the right child is not null |
| 69 | + if(currVal != "N") { |
| 70 | + |
| 71 | + // Create the right child for the current node |
| 72 | + currNode->right = newNode(stoi(currVal)); |
| 73 | + |
| 74 | + // Push it to the queue |
| 75 | + queue.push(currNode->right); |
| 76 | + } |
| 77 | + i++; |
| 78 | + } |
| 79 | + |
| 80 | + return root; |
| 81 | +} |
| 82 | +int kthAncestor(Node *root, int k, int node); |
| 83 | + |
| 84 | +int main() |
| 85 | +{ |
| 86 | + int t; |
| 87 | + scanf("%d ",&t); |
| 88 | + while(t--) |
| 89 | + { |
| 90 | + int k , node; |
| 91 | + scanf("%d ",&k); |
| 92 | + scanf("%d ",&node); |
| 93 | + string s; |
| 94 | + getline(cin,s); |
| 95 | + Node* root = buildTree(s); |
| 96 | + cout<<kthAncestor(root,k,node)<<endl; |
| 97 | + } |
| 98 | + return 0; |
| 99 | +} |
| 100 | + |
| 101 | +// } Driver Code Ends |
| 102 | + |
| 103 | + |
| 104 | +//User function Template for C++ |
| 105 | +/* |
| 106 | +Structure of the node of the binary tree is as |
| 107 | +struct Node |
| 108 | +{ |
| 109 | + int data; |
| 110 | + struct Node *left, *right; |
| 111 | +}; |
| 112 | +*/ |
| 113 | +// your task is to complete this function |
| 114 | +void solve1(Node *root, int k, int node, int &ans, vector<int>v) |
| 115 | +{ |
| 116 | + if(!root) |
| 117 | + return; |
| 118 | + |
| 119 | + v.push_back(root->data); |
| 120 | + if(root->data == node) |
| 121 | + { |
| 122 | + int size=v.size(); |
| 123 | + // cout<<root->data<<" "<<v.size()<<" " <<k<<endl; |
| 124 | + if(size > k) |
| 125 | + { |
| 126 | + // cout<<"HEY"; |
| 127 | + ans = v[((int)v.size() - k - 1)]; |
| 128 | + } |
| 129 | + return; |
| 130 | + } |
| 131 | + solve1(root->left, k, node, ans, v); |
| 132 | + solve1(root->right, k, node, ans, v); |
| 133 | +} |
| 134 | +int kthAncestor(Node *root, int k, int node) |
| 135 | +{ |
| 136 | + int ans=-1; |
| 137 | + solve1(root, k, node, ans, {}); |
| 138 | + return ans; |
| 139 | +} |
0 commit comments