Skip to content

Commit

Permalink
Adding codes for Lecture 65
Browse files Browse the repository at this point in the history
  • Loading branch information
Love Babbar committed Feb 18, 2022
1 parent f26a642 commit 143c83d
Show file tree
Hide file tree
Showing 5 changed files with 817 additions and 0 deletions.
176 changes: 176 additions & 0 deletions Lecture065 Tree Interview Questions Day3/bloodline.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,176 @@
// { Driver Code Starts
//Initial Template for C++

#include <bits/stdc++.h>
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<string> 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<Node *> 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++

/*
structure of the node of the binary tree is as
struct Node
{
int data;
struct Node *left;
struct Node *right;
Node(int x)
{
data = x;
left = NULL;
right = NULL;
}
};
*/
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
161 changes: 161 additions & 0 deletions Lecture065 Tree Interview Questions Day3/kAncestor.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
// { Driver Code Starts
#include <bits/stdc++.h>
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<string> 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<Node*> 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<<kthAncestor(root,k,node)<<endl;
}
return 0;
}
// } Driver Code Ends


//User function Template for C++
/*
Structure of the node of the binary tree is as
struct Node
{
int data;
struct Node *left, *right;
};
*/
// your task is to complete this function
Node* solve(Node* root, int &k, int node) {
//base case
if(root == NULL)
return NULL;

if(root->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;
}
Loading

3 comments on commit 143c83d

@satyamjha09
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wow

@satyamjha09
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

great work

@ABD-code-lab
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice Work

Please sign in to comment.