diff --git a/Lecture065-Trees Questions P3/1-longestBloodline.cpp b/Lecture065-Trees Questions P3/1-longestBloodline.cpp new file mode 100644 index 00000000..b281768e --- /dev/null +++ b/Lecture065-Trees Questions P3/1-longestBloodline.cpp @@ -0,0 +1,150 @@ +//{ Driver Code Starts +//Initial Template for C++ + +#include +using namespace std; + +struct Node +{ + int data; + struct Node *left; + struct Node *right; + + Node(int x) + { + data = x; + left = NULL; + right = NULL; + } +}; + +void printInorder(Node *node) +{ + if (node == NULL) + { + return; + } + printInorder(node->left); + cout << node->data << " "; + printInorder(node->right); +} +Node *buildTree(string str) +{ + // Corner Case + if (str.length() == 0 || str[0] == 'N') + return NULL; + + // Creating vector of strings from input + // string after spliting by space + vector ip; + + istringstream iss(str); + for (string str; iss >> str;) + ip.push_back(str); + + // Create the root of the tree + Node *root = new Node(stoi(ip[0])); + + // Push the root to the queue + queue queue; + queue.push(root); + + // Starting from the second element + int i = 1; + while (!queue.empty() && i < ip.size()) + { + + // Get and remove the front of the queue + Node *currNode = queue.front(); + queue.pop(); + + // Get the current node's value from the string + string currVal = ip[i]; + + // If the left child is not null + if (currVal != "N") + { + + // Create the left child for the current Node + currNode->left = new Node(stoi(currVal)); + + // Push it to the queue + queue.push(currNode->left); + } + + // For the right child + i++; + if (i >= ip.size()) + break; + currVal = ip[i]; + + // If the right child is not null + if (currVal != "N") + { + + // Create the right child for the current node + currNode->right = new Node(stoi(currVal)); + + // Push it to the queue + queue.push(currNode->right); + } + i++; + } + + return root; +} + + +class Solution +{ +public: + // height,sum + pair multiPurpose(Node* root){ + if( root == NULL){ + return {0,0}; + } + pair left = multiPurpose(root -> left); + pair right = multiPurpose(root -> right); + int sum = 0, height = 0; + if( left.first > right.first){ + sum = left.second + root-> data; + height = 1 + left.first; + } + else if( left.first < right.first){ + sum = right.second + root-> data; + height = 1 + right.first; + } + else{ + height = 1 + left.first; + sum = root -> data + max(left.second,right.second); + } + + return {height,sum}; + } + + int sumOfLongRootToLeafPath(Node *root) + { + + pair temp = multiPurpose(root); + + return temp.second; + } +}; + +int main() +{ + + int t; + scanf("%d", &t); + cin.ignore(); + while (t--) + { + string treeString; + getline(cin, treeString); + Node *root = buildTree(treeString); + Solution obj; + int res = obj.sumOfLongRootToLeafPath(root); + cout << res << "\n"; + } + return 0; +} diff --git a/Lecture065-Trees Questions P3/1A-longestBloodline.cpp b/Lecture065-Trees Questions P3/1A-longestBloodline.cpp new file mode 100644 index 00000000..c2816b61 --- /dev/null +++ b/Lecture065-Trees Questions P3/1A-longestBloodline.cpp @@ -0,0 +1,157 @@ +// { Driver Code Starts +//Initial Template for C++ + +#include +using namespace std; + +struct Node +{ + int data; + struct Node *left; + struct Node *right; + + Node(int x) + { + data = x; + left = NULL; + right = NULL; + } +}; + +void printInorder(Node *node) +{ + if (node == NULL) + { + return; + } + printInorder(node->left); + cout << node->data << " "; + printInorder(node->right); +} +Node *buildTree(string str) +{ + // Corner Case + if (str.length() == 0 || str[0] == 'N') + return NULL; + + // Creating vector of strings from input + // string after spliting by space + vector ip; + + istringstream iss(str); + for (string str; iss >> str;) + ip.push_back(str); + + // Create the root of the tree + Node *root = new Node(stoi(ip[0])); + + // Push the root to the queue + queue queue; + queue.push(root); + + // Starting from the second element + int i = 1; + while (!queue.empty() && i < ip.size()) + { + + // Get and remove the front of the queue + Node *currNode = queue.front(); + queue.pop(); + + // Get the current node's value from the string + string currVal = ip[i]; + + // If the left child is not null + if (currVal != "N") + { + + // Create the left child for the current Node + currNode->left = new Node(stoi(currVal)); + + // Push it to the queue + queue.push(currNode->left); + } + + // For the right child + i++; + if (i >= ip.size()) + break; + currVal = ip[i]; + + // If the right child is not null + if (currVal != "N") + { + + // Create the right child for the current node + currNode->right = new Node(stoi(currVal)); + + // Push it to the queue + queue.push(currNode->right); + } + i++; + } + + return root; +} + + +class Solution +{ +public: + + void solve(Node* root, int sum, int &maxSum, int len, int &maxLen) { + //base case + if( root == NULL ) { + + if(len > maxLen) + { + maxLen = len; + maxSum = sum; + } + else if(len == maxLen) + { + maxSum = max(sum, maxSum); + } + return; + } + + sum = sum + root->data; + + solve(root->left, sum, maxSum, len+1, maxLen); + solve(root->right, sum, maxSum, len+1, maxLen); + + } + + int sumOfLongRootToLeafPath(Node *root) + { + int len = 0; + int maxLen = 0; + + int sum = 0; + int maxSum = INT_MIN; + + solve(root, sum, maxSum, len, maxLen); + + return maxSum; + } +}; + +// { Driver Code Starts. + +int main() +{ + + int t; + scanf("%d", &t); + cin.ignore(); + while (t--) + { + string treeString; + getline(cin, treeString); + Node *root = buildTree(treeString); + Solution obj; + int res = obj.sumOfLongRootToLeafPath(root); + cout << res << "\n"; + } + return 0; +} // } Driver Code Ends \ No newline at end of file diff --git a/Lecture065-Trees Questions P3/2-LCAofBT.cpp b/Lecture065-Trees Questions P3/2-LCAofBT.cpp new file mode 100644 index 00000000..7c95da5c --- /dev/null +++ b/Lecture065-Trees Questions P3/2-LCAofBT.cpp @@ -0,0 +1,168 @@ +//{ Driver Code Starts +#include +using namespace std; + +// Tree Node +struct Node +{ + int data; + Node* left; + Node* right; + Node(){ + data = 0; + left = right = NULL; + } + Node(int x){ + data = x; + left = right = NULL; + } +}; + +class Solution +{ + public: + //Function to return the lowest common ancestor in a Binary Tree. + void FindHim(Node* &root, int n1, int n2, vector output , vector &s1, vector &s2){ + if(root == NULL){ + return; + } + output.push_back(root); + if(root -> data == n1){ + // output.push_back(root); + s1 = output; + } + if(root -> data == n2){ + // output.push_back(root); + s2 = output; + } + FindHim(root->left,n1,n2,output,s1,s2); + FindHim(root->right,n1,n2,output,s1,s2); + + } + + Node* lca(Node* root ,int n1 ,int n2 ) + { + + vector s1,s2,ans; + FindHim(root,n1,n2,ans,s1,s2); + int i = 0, j = 0; + int a = s1.size(), b = s2.size(); + while( (i < a && j < b) && s1[i] == s2[j] ){ + i++; + j++; + } + + + + return s1[i-1]; + } +}; + +//{ Driver Code Starts. + +Node* newNode(int val) +{ + Node* temp = new Node; + temp->data = val; + temp->left = NULL; + temp->right = NULL; + + return temp; +} + + +// Function to Build Tree +Node* buildTree(string str) +{ + // Corner Case + if(str.length() == 0 || str[0] == 'N') + return NULL; + + // Creating vector of strings from input + // string after spliting by space + vector ip; + + istringstream iss(str); + for(string str; iss >> str; ) + ip.push_back(str); + + // for(string i:ip) + // cout< queue; + queue.push(root); + + // Starting from the second element + int i = 1; + while(!queue.empty() && i < ip.size()) { + + // Get and remove the front of the queue + Node* currNode = queue.front(); + queue.pop(); + + // Get the current node's value from the string + string currVal = ip[i]; + + // If the left child is not null + if(currVal != "N") { + + // Create the left child for the current node + currNode->left = newNode(stoi(currVal)); + + // Push it to the queue + queue.push(currNode->left); + } + + // For the right child + i++; + if(i >= ip.size()) + break; + currVal = ip[i]; + + // If the right child is not null + if(currVal != "N") { + + // Create the right child for the current node + currNode->right = newNode(stoi(currVal)); + + // Push it to the queue + queue.push(currNode->right); + } + i++; + } + + return root; +} + +// Function for Inorder Traversal +void printInorder(Node* root) +{ + if(!root) + return; + + printInorder(root->left); + cout<data<<" "; + printInorder(root->right); +} + +int main() { + int t; + scanf("%d",&t); + while(t--) + { + int a,b; + scanf("%d %d ",&a,&b); + string s; + getline(cin,s); + Node* root = buildTree(s); + Solution ob; + cout<data< +using namespace std; + +// Tree Node +struct Node +{ + int data; + Node* left; + Node* right; + Node(){ + data = 0; + left = right = NULL; + } + Node(int x){ + data = x; + left = right = NULL; + } +}; + +class Solution +{ + public: + //Function to return the lowest common ancestor in a Binary Tree. + Node* lca(Node* root ,int n1 ,int n2 ) + { + //base case + if(root == NULL) + { + return NULL; + } + + if(root->data == n1 || root->data == n2) + { + return root; + } + + Node* leftAns = lca(root->left, n1, n2); + Node* rightAns = lca(root->right, n1, n2); + + if(leftAns != NULL && rightAns != NULL) + { + return root; + } + else if(leftAns != NULL && rightAns == NULL) + return leftAns; + else if(leftAns == NULL && rightAns != NULL) + return rightAns; + else + return NULL; + } +}; + +// { Driver Code Starts. + +Node* newNode(int val) +{ + Node* temp = new Node; + temp->data = val; + temp->left = NULL; + temp->right = NULL; + + return temp; +} + + +// Function to Build Tree +Node* buildTree(string str) +{ + // Corner Case + if(str.length() == 0 || str[0] == 'N') + return NULL; + + // Creating vector of strings from input + // string after spliting by space + vector ip; + + istringstream iss(str); + for(string str; iss >> str; ) + ip.push_back(str); + + // for(string i:ip) + // cout< queue; + queue.push(root); + + // Starting from the second element + int i = 1; + while(!queue.empty() && i < ip.size()) { + + // Get and remove the front of the queue + Node* currNode = queue.front(); + queue.pop(); + + // Get the current node's value from the string + string currVal = ip[i]; + + // If the left child is not null + if(currVal != "N") { + + // Create the left child for the current node + currNode->left = newNode(stoi(currVal)); + + // Push it to the queue + queue.push(currNode->left); + } + + // For the right child + i++; + if(i >= ip.size()) + break; + currVal = ip[i]; + + // If the right child is not null + if(currVal != "N") { + + // Create the right child for the current node + currNode->right = newNode(stoi(currVal)); + + // Push it to the queue + queue.push(currNode->right); + } + i++; + } + + return root; +} + +// Function for Inorder Traversal +void printInorder(Node* root) +{ + if(!root) + return; + + printInorder(root->left); + cout<data<<" "; + printInorder(root->right); +} + +int main() { + int t; + scanf("%d",&t); + while(t--) + { + int a,b; + scanf("%d %d ",&a,&b); + string s; + getline(cin,s); + Node* root = buildTree(s); + Solution ob; + cout<data< +using namespace std; + +struct Node +{ + int data; + Node *left; + Node *right; + + Node(int val) { + data = val; + left = right = NULL; + } +}; + +// Function to Build Tree +Node *buildTree(string str) +{ + // Corner Case + if (str.length() == 0 || str[0] == 'N') + return NULL; + + // Creating vector of strings from input + // string after spliting by space + vector ip; + + istringstream iss(str); + for (string str; iss >> str;) + ip.push_back(str); + + // Create the root of the tree + Node *root = new Node(stoi(ip[0])); + + // Push the root to the queue + queue queue; + queue.push(root); + + // Starting from the second element + int i = 1; + while (!queue.empty() && i < ip.size()) { + + // Get and remove the front of the queue + Node *currNode = queue.front(); + queue.pop(); + + // Get the current Node's value from the string + string currVal = ip[i]; + + // If the left child is not null + if (currVal != "N") { + + // Create the left child for the current Node + currNode->left = new Node(stoi(currVal)); + + // Push it to the queue + queue.push(currNode->left); + } + + // For the right child + i++; + if (i >= ip.size()) + break; + currVal = ip[i]; + + // If the right child is not null + if (currVal != "N") { + + // Create the right child for the current Node + currNode->right = new Node(stoi(currVal)); + + // Push it to the queue + queue.push(currNode->right); + } + i++; + } + + return root; +} + + +// } Driver Code Ends +//User function template for C++ + +/* +struct Node +{ + int data; + Node *left; + Node *right; + + Node(int val) { + data = val; + left = right = NULL; + } +}; +*/ +class Solution{ + public: + + void solve2(Node* root, int k, long long int sum, int &count){ + + if(root == NULL){ + return; + } + + sum+=root->data; + if(sum == k){ + count++; + } + cout << root -> data << " " << sum << " " << count << endl; + Node* l = root->left; + Node* r = root->right; + solve(r,k,sum,count); + solve(r,k,0,count); + solve(l,k,0,count); + solve(l,k,sum,count); + } + void solve(Node* root, int k, long long int sum, int &count ,int flag = 0){ + + if(root == NULL){ + return; + } + Node* l = root->left; + Node* r = root->right; + if(flag == 0){ + solve(r,k,0,count,0); + solve(l,k,0,count,0); + } + sum+=root->data; + if(sum == k){ + count++; + } + // cout << root -> data << " " << sum << " " << count << endl; + solve(r,k,sum,count,1); + solve(l,k,sum,count,1); + } + + + int sumK(Node *root,int k) + { + vector path; + int count = 0; + solve(root,k,0,count); + return count; + } +}; + +//{ Driver Code Starts. + +int main() { + string tc; + getline(cin, tc); + int t = stoi(tc); + + while(t--) + { + string s ,ch; + getline(cin, s); + Node* root = buildTree(s); + + string key; + getline(cin, key); + int k=stoi(key); + Solution ob; + cout << ob.sumK(root, k) << "\n"; + } + return 0; +} +// } Driver Code Ends \ No newline at end of file diff --git a/Lecture065-Trees Questions P3/3-SlowKSumPath.cpp b/Lecture065-Trees Questions P3/3-SlowKSumPath.cpp new file mode 100644 index 00000000..16f26556 --- /dev/null +++ b/Lecture065-Trees Questions P3/3-SlowKSumPath.cpp @@ -0,0 +1,136 @@ +// ! IMP + + +#include +using namespace std; + +struct Node +{ + int data; + Node *left; + Node *right; + + Node(int val) { + data = val; + left = right = NULL; + } +}; + +// Function to Build Tree +Node *buildTree(string str) +{ + // Corner Case + if (str.length() == 0 || str[0] == 'N') + return NULL; + + // Creating vector of strings from input + // string after spliting by space + vector ip; + + istringstream iss(str); + for (string str; iss >> str;) + ip.push_back(str); + + // Create the root of the tree + Node *root = new Node(stoi(ip[0])); + + // Push the root to the queue + queue queue; + queue.push(root); + + // Starting from the second element + int i = 1; + while (!queue.empty() && i < ip.size()) { + + // Get and remove the front of the queue + Node *currNode = queue.front(); + queue.pop(); + + // Get the current Node's value from the string + string currVal = ip[i]; + + // If the left child is not null + if (currVal != "N") { + + // Create the left child for the current Node + currNode->left = new Node(stoi(currVal)); + + // Push it to the queue + queue.push(currNode->left); + } + + // For the right child + i++; + if (i >= ip.size()) + break; + currVal = ip[i]; + + // If the right child is not null + if (currVal != "N") { + + // Create the right child for the current Node + currNode->right = new Node(stoi(currVal)); + + // Push it to the queue + queue.push(currNode->right); + } + i++; + } + + return root; +} + + +class Solution{ + public: + void solve(Node* root, int k, vector path, int &count){ + if(root == NULL) + return; + + path.push_back(root->data); + solve(root->left,k,path,count); + solve(root->right,k,path,count); + + int size = path.size(); + int sum = 0; + for(int i = size -1; i >=0 ; i--){ + sum+=path[i]; + if(sum ==k){ + count++; + } + path.pop_back(); + } + } + + + int sumK(Node *root,int k) + { + vector path; + int count = 0; + solve(root,k,path,count); + return count; + } +}; + +//{ Driver Code Starts. + +int main() { + string tc; + getline(cin, tc); + int t = stoi(tc); + + while(t--) + { + string s ,ch; + getline(cin, s); + Node* root = buildTree(s); + + string key; + getline(cin, key); + int k=stoi(key); + Solution ob; + cout << ob.sumK(root, k) << "\n"; + } + return 0; +} +// } Driver Code Ends \ No newline at end of file diff --git a/Lecture065-Trees Questions P3/3-fastKsumPath.cpp b/Lecture065-Trees Questions P3/3-fastKsumPath.cpp new file mode 100644 index 00000000..65c66263 --- /dev/null +++ b/Lecture065-Trees Questions P3/3-fastKsumPath.cpp @@ -0,0 +1,132 @@ +// ! Prefix Sum in trees + +#include +using namespace std; + +struct Node +{ + int data; + Node *left; + Node *right; + + Node(int val) { + data = val; + left = right = NULL; + } +}; + +// Function to Build Tree +Node *buildTree(string str) +{ + // Corner Case + if (str.length() == 0 || str[0] == 'N') + return NULL; + + // Creating vector of strings from input + // string after spliting by space + vector ip; + + istringstream iss(str); + for (string str; iss >> str;) + ip.push_back(str); + + // Create the root of the tree + Node *root = new Node(stoi(ip[0])); + + // Push the root to the queue + queue queue; + queue.push(root); + + // Starting from the second element + int i = 1; + while (!queue.empty() && i < ip.size()) { + + // Get and remove the front of the queue + Node *currNode = queue.front(); + queue.pop(); + + // Get the current Node's value from the string + string currVal = ip[i]; + + // If the left child is not null + if (currVal != "N") { + + // Create the left child for the current Node + currNode->left = new Node(stoi(currVal)); + + // Push it to the queue + queue.push(currNode->left); + } + + // For the right child + i++; + if (i >= ip.size()) + break; + currVal = ip[i]; + + // If the right child is not null + if (currVal != "N") { + + // Create the right child for the current Node + currNode->right = new Node(stoi(currVal)); + + // Push it to the queue + queue.push(currNode->right); + } + i++; + } + + return root; +} + +class Solution{ + public: + map notForSlow; + void solve(Node* root, int k, int currSum,int &count){ + if(root == NULL){ + return; + } + currSum+=root->data; + if(notForSlow[currSum - k] > 0){ + count+=notForSlow[currSum - k]; + } + notForSlow[currSum]++; + solve(root->left,k,currSum,count); + solve(root->right,k,currSum,count); + notForSlow[currSum]--; + + } + + + int sumK(Node *root,int k) + { + notForSlow[0]++; + vector path; + int count = 0; + solve(root,k,0,count); + return count; + } +}; + +//{ Driver Code Starts. + +int main() { + string tc; + getline(cin, tc); + int t = stoi(tc); + + while(t--) + { + string s ,ch; + getline(cin, s); + Node* root = buildTree(s); + + string key; + getline(cin, key); + int k=stoi(key); + Solution ob; + cout << ob.sumK(root, k) << "\n"; + } + return 0; +} +// } Driver Code Ends \ No newline at end of file diff --git a/Lecture065-Trees Questions P3/4-KthAncesstor.cpp b/Lecture065-Trees Questions P3/4-KthAncesstor.cpp new file mode 100644 index 00000000..d0e9401d --- /dev/null +++ b/Lecture065-Trees Questions P3/4-KthAncesstor.cpp @@ -0,0 +1,125 @@ +//{ Driver Code Starts +#include +using namespace std; + +struct Node +{ + int data; + struct Node *left; + struct Node *right; +}; +Node* newNode(int val) +{ + Node* temp = new Node; + temp->data = val; + temp->left = NULL; + temp->right = NULL; + + return temp; +} +Node* buildTree(string str) +{ + // Corner Case + if(str.length() == 0 || str[0] == 'N') + return NULL; + + // Creating vector of strings from input + // string after spliting by space + vector ip; + + istringstream iss(str); + for(string str; iss >> str; ) + ip.push_back(str); + + // Create the root of the tree + Node* root = newNode(stoi(ip[0])); + + // Push the root to the queue + queue queue; + queue.push(root); + + // Starting from the second element + int i = 1; + while(!queue.empty() && i < ip.size()) { + + // Get and remove the front of the queue + Node* currNode = queue.front(); + queue.pop(); + + // Get the current node's value from the string + string currVal = ip[i]; + + // If the left child is not null + if(currVal != "N") { + + // Create the left child for the current node + currNode->left = newNode(stoi(currVal)); + + // Push it to the queue + queue.push(currNode->left); + } + + // For the right child + i++; + if(i >= ip.size()) + break; + currVal = ip[i]; + + // If the right child is not null + if(currVal != "N") { + + // Create the right child for the current node + currNode->right = newNode(stoi(currVal)); + + // Push it to the queue + queue.push(currNode->right); + } + i++; + } + + return root; +} +int kthAncestor(Node *root, int k, int node); + +int main() +{ + int t; + scanf("%d ",&t); + while(t--) + { + int k , node; + scanf("%d ",&k); + scanf("%d ",&node); + string s; + getline(cin,s); + Node* root = buildTree(s); + cout< temp,vector &peedhi ){ + if(root == NULL){ + return; + } + if( root -> data == n){ + peedhi = temp; + return; + } + temp.push_back(root->data); + solve(root->left,n,temp,peedhi); + solve(root->right,n,temp,peedhi); +} + +int kthAncestor(Node *root, int k, int node) +{ + vector peedhi,temp; + solve(root,node,temp,peedhi); + int ans = -1; + if(k <= peedhi.size() ) + ans = peedhi[peedhi.size() - k]; + + + + return ans; +} diff --git a/Lecture065-Trees Questions P3/4-SC-1KthAncesstor.cpp b/Lecture065-Trees Questions P3/4-SC-1KthAncesstor.cpp new file mode 100644 index 00000000..1bdc53b1 --- /dev/null +++ b/Lecture065-Trees Questions P3/4-SC-1KthAncesstor.cpp @@ -0,0 +1,160 @@ +//{ Driver Code Starts +#include +using namespace std; + +struct Node +{ + int data; + struct Node *left; + struct Node *right; +}; +Node* newNode(int val) +{ + Node* temp = new Node; + temp->data = val; + temp->left = NULL; + temp->right = NULL; + + return temp; +} +Node* buildTree(string str) +{ + // Corner Case + if(str.length() == 0 || str[0] == 'N') + return NULL; + + // Creating vector of strings from input + // string after spliting by space + vector ip; + + istringstream iss(str); + for(string str; iss >> str; ) + ip.push_back(str); + + // Create the root of the tree + Node* root = newNode(stoi(ip[0])); + + // Push the root to the queue + queue queue; + queue.push(root); + + // Starting from the second element + int i = 1; + while(!queue.empty() && i < ip.size()) { + + // Get and remove the front of the queue + Node* currNode = queue.front(); + queue.pop(); + + // Get the current node's value from the string + string currVal = ip[i]; + + // If the left child is not null + if(currVal != "N") { + + // Create the left child for the current node + currNode->left = newNode(stoi(currVal)); + + // Push it to the queue + queue.push(currNode->left); + } + + // For the right child + i++; + if(i >= ip.size()) + break; + currVal = ip[i]; + + // If the right child is not null + if(currVal != "N") { + + // Create the right child for the current node + currNode->right = newNode(stoi(currVal)); + + // Push it to the queue + queue.push(currNode->right); + } + i++; + } + + return root; +} +int kthAncestor(Node *root, int k, int node); + +int main() +{ + int t; + scanf("%d ",&t); + while(t--) + { + int k , node; + scanf("%d ",&k); + scanf("%d ",&node); + string s; + getline(cin,s); + Node* root = buildTree(s); + cout< solve(Node* root, int k, int node){ + if(root == NULL){ + return {0,-1}; + } + if(root -> data == node){ + return {node,0}; + } + + pair left = solve(root->left,k,node); + pair right = solve(root->right,k,node); + // cout << root -> data << endl; + // cout << left.first << " " << left.second << endl; + // cout << right.first << " " << right.second << endl; + if(left.second != -1){ + if(left.second < k){ + return {root->data,left.second+1}; + } + else{ + return {left.first,k}; + } + } + else if(right.second != -1){ + // cout << root -> data << endl; + if(right.second < k){ + return {root->data,right.second+1}; + } + else{ + return {right.first,k}; + } + } + else{ + return {0,-1}; + } + +} + +int kthAncestor(Node *root, int k, int node) +{ + pair ans = solve(root,k,node); + // cout << ans.first << " " < +using namespace std; + +struct Node +{ + int data; + struct Node *left; + struct Node *right; +}; +Node* newNode(int val) +{ + Node* temp = new Node; + temp->data = val; + temp->left = NULL; + temp->right = NULL; + + return temp; +} +Node* buildTree(string str) +{ + // Corner Case + if(str.length() == 0 || str[0] == 'N') + return NULL; + + // Creating vector of strings from input + // string after spliting by space + vector ip; + + istringstream iss(str); + for(string str; iss >> str; ) + ip.push_back(str); + + // Create the root of the tree + Node* root = newNode(stoi(ip[0])); + + // Push the root to the queue + queue queue; + queue.push(root); + + // Starting from the second element + int i = 1; + while(!queue.empty() && i < ip.size()) { + + // Get and remove the front of the queue + Node* currNode = queue.front(); + queue.pop(); + + // Get the current node's value from the string + string currVal = ip[i]; + + // If the left child is not null + if(currVal != "N") { + + // Create the left child for the current node + currNode->left = newNode(stoi(currVal)); + + // Push it to the queue + queue.push(currNode->left); + } + + // For the right child + i++; + if(i >= ip.size()) + break; + currVal = ip[i]; + + // If the right child is not null + if(currVal != "N") { + + // Create the right child for the current node + currNode->right = newNode(stoi(currVal)); + + // Push it to the queue + queue.push(currNode->right); + } + i++; + } + + return root; +} +int kthAncestor(Node *root, int k, int node); + +int main() +{ + int t; + scanf("%d ",&t); + while(t--) + { + int k , node; + scanf("%d ",&k); + scanf("%d ",&node); + string s; + getline(cin,s); + Node* root = buildTree(s); + cout<data == node) + { + return root; + } + + Node* leftAns = solve(root->left, k, node); + Node* rightAns = solve(root->right, k, node); + + + //wapas + if(leftAns != NULL && rightAns == NULL) + { + k--; + if(k<=0) + { + //answer lock + k = INT_MAX; + return root; + } + return leftAns; + } + + if(leftAns == NULL && rightAns != NULL) { + k--; + if(k<=0) + { + //answer lock + k = INT_MAX; + return root; + } + return rightAns; + } + return NULL; + + +} +int kthAncestor(Node *root, int k, int node) +{ + Node* ans = solve(root, k, node); + if(ans == NULL || ans->data == node) + return -1; + else + return ans->data; +} \ No newline at end of file diff --git a/Lecture065-Trees Questions P3/5-MaxNonAdjacentSum.cpp b/Lecture065-Trees Questions P3/5-MaxNonAdjacentSum.cpp new file mode 100644 index 00000000..9cca83bc --- /dev/null +++ b/Lecture065-Trees Questions P3/5-MaxNonAdjacentSum.cpp @@ -0,0 +1,130 @@ +// ! IMP +#include +using namespace std; + +// Tree Node +struct Node +{ + int data; + Node* left; + Node* right; +}; + +// Utility function to create a new Tree Node +Node* newNode(int val) +{ + Node* temp = new Node; + temp->data = val; + temp->left = NULL; + temp->right = NULL; + + return temp; +} + +// Function to Build Tree +Node* buildTree(string str) +{ + // Corner Case + if(str.length() == 0 || str[0] == 'N') + return NULL; + + // Creating vector of strings from input + // string after spliting by space + vector ip; + + istringstream iss(str); + for(string str; iss >> str; ) + ip.push_back(str); + + // Create the root of the tree + Node* root = newNode(stoi(ip[0])); + + // Push the root to the queue + queue queue; + queue.push(root); + + // Starting from the second element + int i = 1; + while(!queue.empty() && i < ip.size()) { + + // Get and remove the front of the queue + Node* currNode = queue.front(); + queue.pop(); + + // Get the current node's value from the string + string currVal = ip[i]; + + // If the left child is not null + if(currVal != "N") { + + // Create the left child for the current node + currNode->left = newNode(stoi(currVal)); + + // Push it to the queue + queue.push(currNode->left); + } + + // For the right child + i++; + if(i >= ip.size()) + break; + currVal = ip[i]; + + // If the right child is not null + if(currVal != "N") { + + // Create the right child for the current node + currNode->right = newNode(stoi(currVal)); + + // Push it to the queue + queue.push(currNode->right); + } + i++; + } + + return root; +} + +class Solution{ + public: + + // inc, not inc + pair solve(Node*root){ + if(root == NULL){ + pair n = make_pair(0,0); + return n; + } + + pair left = solve(root->left); + pair right = solve(root->right); + + int inc = root->data + left.second + right.second; + int not_inc = max(left.first,left.second) + max(right.first,right.second); + + return {inc,not_inc}; + } + + + int getMaxSum(Node *root) + { + pair ans = solve(root); + + return max(ans.first,ans.second); + } +}; + +int main() +{ + int t; + scanf("%d ",&t); + while (t--) + { + string s; + getline(cin,s); + Node* root = buildTree(s); + Solution ob; + cout< +using namespace std; + +struct Node +{ + int data; + struct Node *left; + struct Node *right; + + Node(int x){ + data = x; + left = NULL; + right = NULL; + } +}; +void printPostOrder(Node *root) +{ + if(root==NULL) + return; + printPostOrder(root->left); + printPostOrder(root->right); + cout<data<<" "; +} + +class Solution{ + public: + + Node* buildTree(int in[],int pre[], int n) + { + if( n == 0 ){ + return NULL; + } + + if( n == 1){ + Node* ans = new Node(in[0]); + return ans; + } + + + + // ! to optimize we can use map + // ! | + // ! \/ + int i = 0; + while( in[i] != pre[0]){ + i++; + } + + + Node* ans = new Node(pre[0]); + ans -> left = buildTree(in,pre+1,i); + ans -> right = buildTree(in+i+1,pre+1+i,n-i-1); + return ans; + } +}; + +//{ Driver Code Starts. +int main() +{ + int t; + cin>>t; + while(t--) + { + int n; + cin>>n; + + int inorder[n], preorder[n]; + for(int i=0; i> inorder[i]; + for(int i=0; i> preorder[i]; + Solution obj; + Node *root = obj.buildTree(inorder, preorder, n); + printPostOrder(root); + cout<< endl; + } +} + +// } Driver Code Ends \ No newline at end of file diff --git a/Lecture066-Construct A BT/2-constructBT_InandPost.cpp b/Lecture066-Construct A BT/2-constructBT_InandPost.cpp new file mode 100644 index 00000000..1aeef604 --- /dev/null +++ b/Lecture066-Construct A BT/2-constructBT_InandPost.cpp @@ -0,0 +1,69 @@ +#include +using namespace std; + +struct Node { + int data; + struct Node* left; + struct Node* right; + + Node(int x) { + data = x; + left = right = NULL; + } +}; + +void preOrder(Node* node) { + if (node == NULL) return; + + /* then print the data of node */ + printf("%d ", node->data); + + /* first recur on left child */ + preOrder(node->left); + + /* now recur on right child */ + preOrder(node->right); +} + +Node* buildTree(int in[], int post[], int n); + +int main() { + int t, n; + cin >> t; + while (t--) { + cin >> n; + int in[n], post[n]; + for (int i = 0; i < n; i++) cin >> in[i]; + for (int i = 0; i < n; i++) cin >> post[i]; + Node* root = buildTree(in, post, n); + preOrder(root); + cout << endl; + } + return 0; +} + +Node *buildTree(int in[], int post[], int n) { + if( n == 0){ + return NULL; + } + + if(n == 1){ + Node* ans = new Node(post[n-1]); + return ans; + } + + Node* ans = new Node(post[n-1]); + + // ! to optimize we can use map + // ! | + // ! \/ + int i = 0; + while( in[i] != post[n-1]){ + i++; + } + + ans -> left = buildTree(in,post,i); + ans -> right = buildTree(in+i+1,post+i,n-i-1); + + return ans; +} diff --git a/Lecture067-Burn A Tree/Hard.cpp b/Lecture067-Burn A Tree/Hard.cpp new file mode 100644 index 00000000..533f898a --- /dev/null +++ b/Lecture067-Burn A Tree/Hard.cpp @@ -0,0 +1,175 @@ +// ! T-IMP +#include +using namespace std; + +struct Node { + int data; + Node *left; + Node *right; + + Node(int val) { + data = val; + left = right = NULL; + } +}; + + +Node *buildTree(string str) { + // Corner Case + if (str.length() == 0 || str[0] == 'N') + return NULL; + + // Creating vector of strings from input + // string after spliting by space + vector ip; + + istringstream iss(str); + for (string str; iss >> str;) + ip.push_back(str); + + // Create the root of the tree + Node *root = new Node(stoi(ip[0])); + + // Push the root to the queue + queue queue; + queue.push(root); + + // Starting from the second element + int i = 1; + while (!queue.empty() && i < ip.size()) { + + // Get and remove the front of the queue + Node *currNode = queue.front(); + queue.pop(); + + // Get the current Node's value from the string + string currVal = ip[i]; + + // If the left child is not null + if (currVal != "N") { + + // Create the left child for the current Node + currNode->left = new Node(stoi(currVal)); + + // Push it to the queue + queue.push(currNode->left); + } + + // For the right child + i++; + if (i >= ip.size()) + break; + currVal = ip[i]; + + // If the right child is not null + if (currVal != "N") { + + // Create the right child for the current Node + currNode->right = new Node(stoi(currVal)); + + // Push it to the queue + queue.push(currNode->right); + } + i++; + } + + return root; +} + +class Solution { + public: + Node* findingDaddy(Node* root,Node* papa, map &parent,int target){ + if( root == NULL) + return NULL; + + parent[root] = papa; + Node* left = findingDaddy(root->left,root,parent,target); + Node* right = findingDaddy(root->right,root,parent,target); + + if(root-> data == target) + return root; + + if(left != NULL) + return left; + + if(right != NULL) + return right; + + return NULL; + } + + int minTime(Node* root, int target) + { + if(root == NULL){ + return 0; + } + map parent; + Node* tNode = findingDaddy(root,NULL,parent,target); + int time = 0; + map visited ; + visited[tNode] = true; + // cout << parent.size() << " "; + queue q; + q.push(tNode); + // cout << q.size(); + while(!q.empty()){ + int flag = 0; + int size = q.size(); + for( int i = 0; i < size; i++){ + Node*temp = q.front(); + q.pop(); + if(parent[temp] != NULL && visited[parent[temp]] == false){ + q.push(parent[temp]); + flag = 1; + } + + if(temp->left != NULL && visited[temp->left] == false){ + q.push(temp->left); + flag = 1; + } + if(temp->right != NULL && visited[temp->right] == false){ + q.push(temp->right); + flag = 1; + } + + + visited[parent[temp]] = true; + visited[temp->left] = true; + visited[temp->right] = true; + } + if(flag) + time++; + + } + return time; + } +}; + +//{ Driver Code Starts. + +int main() +{ + int tc; + scanf("%d ", &tc); + while (tc--) + { + string treeString; + getline(cin, treeString); + // cout<>target; + // cout< +using namespace std; + +// A binary tree Node has data, pointer to left child +// and a pointer to right child +struct Node { + int data; + struct Node* left; + struct Node* right; + Node(int data) + { + this->data = data; + left = right = NULL; + } +}; + +//Iterative function for inorder tree traversal +void inOrder(struct Node* root) +{ + stack s; + Node* curr = root; + + while (curr != NULL || s.empty() == false) { + + // Reach the left most Node of the + // curr Node + while (curr != NULL) { + + // Place pointer to a tree node on + // the stack before traversing + // the node's left subtree + s.push(curr); + curr = curr->left; + } + + // Current must be NULL at this point + curr = s.top(); + s.pop(); + + cout << curr->data << " "; + + // we have visited the node and its + // left subtree. Now, it's right + // subtree's turn + curr = curr->right; + + } +} + +// Driver program to test above functions +int main() +{ + /* Constructed binary tree is + 1 + / \ + 2 3 + / \ + 4 5 + */ + struct Node* root = new Node(1); + root->left = new Node(2); + root->right = new Node(3); + root->left->left = new Node(4); + root->left->right = new Node(5); + + inOrder(root); + return 0; +} diff --git a/Lecture068-Morris Traversal & Flatten Tree/2-MorrisT.cpp b/Lecture068-Morris Traversal & Flatten Tree/2-MorrisT.cpp new file mode 100644 index 00000000..ea28a18c --- /dev/null +++ b/Lecture068-Morris Traversal & Flatten Tree/2-MorrisT.cpp @@ -0,0 +1,88 @@ +// ! IMP Inorder traversal S.C O(1) + +#include +using namespace std; + +struct tNode { + int data; + struct tNode* left; + struct tNode* right; +}; + +void MorrisTraversal(struct tNode* root) +{ + struct tNode *current, *pre; + + if (root == NULL) + return; + + current = root; + while (current != NULL) { + + if (current->left == NULL) { + cout << current->data << " "; + current = current->right; + } + else { + + /* Find the inorder predecessor of current */ + pre = current->left; + while (pre->right != NULL + && pre->right != current) + pre = pre->right; + + /* Make current as the right child of its + inorder predecessor */ + if (pre->right == NULL) { + pre->right = current; + current = current->left; + } + + /* Revert the changes made in the 'if' part to + restore the original tree i.e., fix the right + child of predecessor */ + else { + pre->right = NULL; + cout << current->data << " "; + current = current->right; + } /* End of if condition pre->right == NULL */ + } /* End of if condition current->left == NULL*/ + } /* End of while */ +} + +/* UTILITY FUNCTIONS */ +/* Helper function that allocates a new tNode with the +given data and NULL left and right pointers. */ +struct tNode* newtNode(int data) +{ + struct tNode* node = new tNode; + node->data = data; + node->left = NULL; + node->right = NULL; + + return (node); +} + +/* Driver program to test above functions*/ +int main() +{ + + /* Constructed binary tree is + 1 + / \ + 2 3 + / \ + 4 5 +*/ + struct tNode* root = newtNode(1); + root->left = newtNode(2); + root->right = newtNode(3); + root->left->left = newtNode(4); + root->left->right = newtNode(5); + + MorrisTraversal(root); + + return 0; +} + +// This code is contributed by Sania Kumari Gupta (kriSania804) diff --git a/Lecture068-Morris Traversal & Flatten Tree/3-FlattenBT.cpp b/Lecture068-Morris Traversal & Flatten Tree/3-FlattenBT.cpp new file mode 100644 index 00000000..bfdc8168 --- /dev/null +++ b/Lecture068-Morris Traversal & Flatten Tree/3-FlattenBT.cpp @@ -0,0 +1,150 @@ + +#include +using namespace std; + +struct Node { + int key; + Node *left, *right; +}; + + +Node* newNode(int key) +{ + Node* node = new Node; + node->key = key; + node->left = node->right = NULL; + return (node); +} + +Node* buildTree(string str) +{ + // Corner Case + if (str.length() == 0 || str[0] == 'N') + return NULL; + + // Creating vector of strings from input + // string after spliting by space + vector ip; + + istringstream iss(str); + for (string str; iss >> str; ) + ip.push_back(str); + + // Create the root of the tree + Node* root = newNode(stoi(ip[0])); + + // Push the root to the queue + queue queue; + queue.push(root); + + // Starting from the second element + int i = 1; + while (!queue.empty() && i < ip.size()) { + + // Get and remove the front of the queue + Node* currNode = queue.front(); + queue.pop(); + + // Get the current node's value from the string + string currVal = ip[i]; + + // If the left child is not null + if (currVal != "N") { + + // Create the left child for the current node + currNode->left = newNode(stoi(currVal)); + + // Push it to the queue + queue.push(currNode->left); + } + + // For the right child + i++; + if (i >= ip.size()) + break; + currVal = ip[i]; + + // If the right child is not null + if (currVal != "N") { + + // Create the right child for the current node + currNode->right = newNode(stoi(currVal)); + + // Push it to the queue + queue.push(currNode->right); + } + i++; + } + + return root; +} + +void inorder(struct Node* root) +{ + // base condition + if (root == NULL) + return; + inorder(root->left); + cout << root->key << " "; + inorder(root->right); +} + +class Solution +{ + + public: + void flatten(Node *root) + { + Node* curr = root; + while(curr != NULL ){ + if(curr -> left == NULL){ + curr = curr -> right; + } + else{ + Node* pre = curr->left; + Node* store = pre; + while(pre -> right != NULL && pre -> right != curr) + pre = pre -> right; + + if(pre -> right == NULL){ + pre -> right = curr; + curr = curr -> left; + } + else{ + curr -> left = NULL; + Node* temp = curr -> right; + curr -> right = store; + pre -> right = temp; + curr = pre; + } + } + + } + } +}; + +int main() { + + int t; + cin >> t; + getchar(); + + while (t--) + { + + string str; + getline(cin, str); + + Node *root = buildTree(str); + Solution ob; + ob.flatten(root); + inorder(root); + + + cout << "\n"; + } + + return 0; +} + +// } Driver Code Ends \ No newline at end of file diff --git a/Lecture068-Morris Traversal & Flatten Tree/3A-RecursiveFlattenBT.cpp b/Lecture068-Morris Traversal & Flatten Tree/3A-RecursiveFlattenBT.cpp new file mode 100644 index 00000000..7858e743 --- /dev/null +++ b/Lecture068-Morris Traversal & Flatten Tree/3A-RecursiveFlattenBT.cpp @@ -0,0 +1,148 @@ +//{ Driver Code Starts +//Initial Template for C++ + +#include +using namespace std; + +struct Node { + int key; + Node *left, *right; +}; + +/* utility that allocates a new Node + with the given key */ +Node* newNode(int key) +{ + Node* node = new Node; + node->key = key; + node->left = node->right = NULL; + return (node); +} + +Node* buildTree(string str) +{ + // Corner Case + if (str.length() == 0 || str[0] == 'N') + return NULL; + + // Creating vector of strings from input + // string after spliting by space + vector ip; + + istringstream iss(str); + for (string str; iss >> str; ) + ip.push_back(str); + + // Create the root of the tree + Node* root = newNode(stoi(ip[0])); + + // Push the root to the queue + queue queue; + queue.push(root); + + // Starting from the second element + int i = 1; + while (!queue.empty() && i < ip.size()) { + + // Get and remove the front of the queue + Node* currNode = queue.front(); + queue.pop(); + + // Get the current node's value from the string + string currVal = ip[i]; + + // If the left child is not null + if (currVal != "N") { + + // Create the left child for the current node + currNode->left = newNode(stoi(currVal)); + + // Push it to the queue + queue.push(currNode->left); + } + + // For the right child + i++; + if (i >= ip.size()) + break; + currVal = ip[i]; + + // If the right child is not null + if (currVal != "N") { + + // Create the right child for the current node + currNode->right = newNode(stoi(currVal)); + + // Push it to the queue + queue.push(currNode->right); + } + i++; + } + + return root; +} + +void inorder(struct Node* root) +{ + // base condition + if (root == NULL) + return; + inorder(root->left); + cout << root->key << " "; + inorder(root->right); +} + + +// } Driver Code Ends +//User function Template for C++ +class Solution +{ + Node* partByPart(Node* root){ + + if(root==NULL || (root -> left == NULL && root -> right == NULL)){ + return root; + } + Node*temp = root-> right; + Node* curr = root; + root -> right = partByPart(root->left); + while(curr -> right != NULL) + curr = curr -> right; + + curr-> right = partByPart(temp); + root -> left = NULL; + return root; + } + + public: + void flatten(Node *root) + { + Node* Ans = partByPart(root); + } +}; + +//{ Driver Code Starts. +int main() { + + int t; + cin >> t; + getchar(); + + while (t--) + { + + string str; + getline(cin, str); + + Node *root = buildTree(str); + Solution ob; + ob.flatten(root); + inorder(root); + + + cout << "\n"; + } + + return 0; +} + +// } Driver Code Ends \ No newline at end of file diff --git a/Lecture069 - BST/1-Implementation.cpp b/Lecture069 - BST/1-Implementation.cpp new file mode 100644 index 00000000..93853406 --- /dev/null +++ b/Lecture069 - BST/1-Implementation.cpp @@ -0,0 +1,139 @@ +#include +using namespace std; + +class Node{ + public: + int data; + Node* left; + Node* right; + + Node(int x){ + this->data = x; + this->left = NULL; + this->right = NULL; + } +}; + +void levelOrderTraversal(Node* root){ + queue q; + q.push(root); + q.push(NULL); + while (!q.empty()){ + Node* temp = q.front(); + q.pop(); + if( temp == NULL){ + cout << endl; + if(!q.empty()){ + q.push(NULL); + } + } + else{ + cout << temp -> data << " "; + if(temp -> left){ + q.push(temp -> left); + } + else{ + // cout << "N "; + } + if(temp -> right){ + q.push(temp -> right); + } + else{ + // cout << "N "; + } + // q.push(NULL); + } + } + + +} + +Node* Insert(Node* root, int x){ + if(root == NULL){ + Node* temp = new Node(x); + return temp; + } + int t = root->data; + if( t > x) + root -> left = Insert(root->left,x); + else + root -> right = Insert(root->right,x); + + return root; +} + +int maxValue(Node* root){ + while( root -> right != NULL) + root = root -> right; + + return root -> data; +} + +int minValue(Node* root){ + while( root -> left != NULL) + root = root -> left; + + return root -> data; +} + +Node* Delete(Node* root, int x){ + if(root == NULL){ + return NULL; + } + + if( root -> data == x){ + // 0 child + if( root -> left == NULL && root -> right == NULL){ + delete root; + return NULL; + } + + // 1 child + if( root -> left == NULL && root -> right != NULL){ + Node* temp = root -> right; + delete root; + return temp; + } + if( root -> left != NULL && root -> right == NULL){ + Node* temp = root -> left; + delete root; + return temp; + } + + // 2 children + if( root -> left != NULL && root -> right != NULL){ + int mini = minValue(root -> right); + root -> data = mini; + root -> right = Delete(root -> right, mini); + return root; + } + + } + else if( root -> data > x ){ + root -> left = Delete(root -> left,x); + return root; + } + root -> right = Delete(root -> right,x); + return root; + + +} + +void takeInput(Node* &root){ + int d; + cin >> d; + while(d != -1){ + root = Insert(root,d); + cin >> d; + } +} + +int main(){ + Node* root = NULL; + takeInput(root); + levelOrderTraversal(root); + root = Delete(root,5); + levelOrderTraversal(root); + +return 0; +} \ No newline at end of file diff --git a/Lecture069 - BST/1-Implementation.exe b/Lecture069 - BST/1-Implementation.exe new file mode 100644 index 00000000..85de7cac Binary files /dev/null and b/Lecture069 - BST/1-Implementation.exe differ diff --git a/Lecture069 - BST/2-search.cpp b/Lecture069 - BST/2-search.cpp new file mode 100644 index 00000000..e837bf40 --- /dev/null +++ b/Lecture069 - BST/2-search.cpp @@ -0,0 +1,37 @@ +#include +using namespace std; + +template + class BinaryTreeNode + { + public: + T data; + BinaryTreeNode *left, *right; + BinaryTreeNode() : data(0), left(NULL), right(NULL) {} + BinaryTreeNode(T x) : data(x), left(NULL), right(NULL) {} + BinaryTreeNode(T x, BinaryTreeNode *left, BinaryTreeNode *right) : data(x), left(left), right(right) {} + }; + +bool searchInBST(BinaryTreeNode *root, int x) { + while(root != NULL){ + int t = root -> data; + if( t == x){ + return true; + } + else if( t > x){ + root = root -> left; + } + else{ + root = root -> right; + } + } + + return false; +} + +int main(){ + + + +return 0; +} \ No newline at end of file diff --git a/Lecture070-BST Questions/1-validateBST.cpp b/Lecture070-BST Questions/1-validateBST.cpp new file mode 100644 index 00000000..9c81a912 --- /dev/null +++ b/Lecture070-BST Questions/1-validateBST.cpp @@ -0,0 +1,35 @@ +// Approach 1 inorder traversal iss sorted +// Approach 2 left subtree all nodes contains value less than root and right has greater +#include +using namespace std; +template + +class BinaryTreeNode +{ +public : + T data; + BinaryTreeNode *left; + BinaryTreeNode *right; + + BinaryTreeNode(T data) { + this -> data = data; + left = NULL; + right = NULL; + } +}; + + +bool isBST(BinaryTreeNode *root, int up, int low){ + if(root == NULL){ + return true; + } + bool check = (root -> data >= low) && (root -> data <= up); + bool left = isBST(root->left,root->data,low); + bool right = isBST(root -> right,up,root->data); + return left && right && check; +} + + +bool validateBST(BinaryTreeNode *root) { + return isBST(root,INT_MAX,INT_MIN); +} \ No newline at end of file diff --git a/Lecture070-BST Questions/2-KthSmallesrElmt.cpp b/Lecture070-BST Questions/2-KthSmallesrElmt.cpp new file mode 100644 index 00000000..335d5934 --- /dev/null +++ b/Lecture070-BST Questions/2-KthSmallesrElmt.cpp @@ -0,0 +1,42 @@ +#include +using namespace std; +template + +class BinaryTreeNode +{ +public : + T data; + BinaryTreeNode *left; + BinaryTreeNode *right; + + BinaryTreeNode(T data) { + this -> data = data; + left = NULL; + right = NULL; + } +}; + +int solve(BinaryTreeNode* root, int &k){ + if(root == NULL){ + return -1; + } + int left = solve(root -> left,k); + if( k == 0){ + return left; + } + k--; + // cout << root -> data << " " << k << endl; + if( k == 0){ + return root->data; + } + int right = solve(root->right,k); + if( k == 0){ + return right; + } + return -1; +} + +int kthSmallest(BinaryTreeNode* root, int k) { + + return solve(root,k); +} \ No newline at end of file diff --git a/Lecture070-BST Questions/3-Lca.cpp b/Lecture070-BST Questions/3-Lca.cpp new file mode 100644 index 00000000..b6948272 --- /dev/null +++ b/Lecture070-BST Questions/3-Lca.cpp @@ -0,0 +1,38 @@ +#include +using namespace std; + +class TreeNode +{ +public: + int data; + TreeNode *left, *right; + TreeNode() : data(0), left(NULL), right(NULL) {} + TreeNode(int x) : data(x), left(NULL), right(NULL) {} + TreeNode(int x, TreeNode *left, TreeNode *right) : data(x), left(left), right(right) {} +}; + + +TreeNode *find(TreeNode *root, TreeNode *p, TreeNode *q){ + if(root == NULL){ + return NULL; + } + if(p->data <= root->data && q -> data >= root->data){ + return root; + } + if(p->data > root -> data){ + return find(root->right, p, q); + } + return find(root->left, p, q); +} + + +TreeNode *LCAinaBST(TreeNode *root, TreeNode *p, TreeNode *q) +{ + if(p -> data < q -> data){ + return find(root,p,q); + } + if(p -> data < q -> data){ + return find(root,p,q); + } + return root; +} \ No newline at end of file diff --git a/Lecture070-BST Questions/4-Presucc.cpp b/Lecture070-BST Questions/4-Presucc.cpp new file mode 100644 index 00000000..acb83834 --- /dev/null +++ b/Lecture070-BST Questions/4-Presucc.cpp @@ -0,0 +1,65 @@ +#include +#include +using namespace std; + + class TreeNode +{ +public: + int data; + TreeNode *left, *right; + TreeNode() : data(0), left(NULL), right(NULL) {} + TreeNode(int x) : data(x), left(NULL), right(NULL) {} + TreeNode(int x, TreeNode *left, TreeNode *right) : data(x), left(left), right(right) {} +}; + + + + +pair predecessorSuccessor(TreeNode *root, int key) +{ + vector ans; + TreeNode* q = root; + int i; + ans.push_back(-1); + while(q != NULL){ + if(q -> left == NULL){ + if (q->data <= key) { + i = ans.size(); + } + ans.push_back(q -> data); + // cout << q -> data << " "; + q = q-> right; + } + else{ + TreeNode* temp = q -> left; + while(temp -> right != NULL && temp -> right != q){ + temp = temp -> right; + } + if(temp -> right == NULL){ + temp -> right = q; + q = q -> left; + } + else{ + if (q->data <= key) { + i = ans.size(); + } + // ans.push_back(temp->data); + // cout << temp -> data << " "; + // cout << q -> data << " "; + ans.push_back(q->data); + q = q -> right; + temp->right = NULL; + } + } + } + ans.push_back(-1); + // for( int i = 0; i < ans.size(); i++) + // cout << ans [i] << " "; + + // cout<< endl << i << endl; + if(ans[i] == key) + return{ans[i-1],ans[i+1]}; + + return{ans[i],ans[i+1]}; + +} \ No newline at end of file diff --git a/Lecture071- BST 3/1-TwoSum.cpp b/Lecture071- BST 3/1-TwoSum.cpp new file mode 100644 index 00000000..66708fa8 --- /dev/null +++ b/Lecture071- BST 3/1-TwoSum.cpp @@ -0,0 +1,41 @@ +#include +using namespace std; +template +class BinaryTreeNode { + public : + T data; + BinaryTreeNode *left; + BinaryTreeNode *right; + + BinaryTreeNode(T data) { + this -> data = data; + left = NULL; + right = NULL; + } + +}; + +void Inorder(BinaryTreeNode* root, vector &storage){ + if( root == NULL){ + return; + } + Inorder(root->left,storage); + storage.push_back(root->data); + Inorder(root->right,storage); +} + + +bool twoSumInBST(BinaryTreeNode* root, int target) { + vector storage; + Inorder(root,storage); + int i = 0, j = storage.size()-1; + while(i < j){ + if(storage[i] + storage[j] == target) + return true; + else if(storage[i] + storage[j] < target ) + i++; + else + j--; + } + return 0; +} \ No newline at end of file diff --git a/Lecture071- BST 3/2-FlattenABst.cpp b/Lecture071- BST 3/2-FlattenABst.cpp new file mode 100644 index 00000000..e69de29b diff --git a/Lecture071- BST 3/3-NormalToBalanced.cpp b/Lecture071- BST 3/3-NormalToBalanced.cpp new file mode 100644 index 00000000..e69de29b diff --git a/Lecture071- BST 3/4-BSTfromPreorder.cpp b/Lecture071- BST 3/4-BSTfromPreorder.cpp new file mode 100644 index 00000000..b759efe9 --- /dev/null +++ b/Lecture071- BST 3/4-BSTfromPreorder.cpp @@ -0,0 +1,48 @@ +#include +using namespace std; +template + + class BinaryTreeNode { + public : + T data; + BinaryTreeNode *left; + BinaryTreeNode *right; + + BinaryTreeNode(T data) { + this -> data = data; + left = NULL; + right = NULL; + } + + ~BinaryTreeNode() { + if (left){ + delete left; + } + if (right){ + delete right; + } + } + }; + + +BinaryTreeNode* solve(vector &pre,int mini, int maxi, int &i){ + // cout << 1 ; + if(i >= pre.size()){ + return NULL; + } + if(pre[i] > maxi || pre[i] < mini) + return NULL; + + + BinaryTreeNode* root = new BinaryTreeNode(pre[i++]); + root -> left = solve(pre,mini,root -> data,i); + root -> right = solve(pre,root -> data,maxi,i); + return root; + +} + +BinaryTreeNode* preorderToBST(vector &preorder) { + + int maxi = INT_MAX, mini = INT_MIN,i=0; + return solve(preorder,mini,maxi,i); +} \ No newline at end of file