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