Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding C++ code files till Lecture 71 #533

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
150 changes: 150 additions & 0 deletions Lecture065-Trees Questions P3/1-longestBloodline.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
//{ 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;
}


class Solution
{
public:
// height,sum
pair<int,int> multiPurpose(Node* root){
if( root == NULL){
return {0,0};
}
pair<int,int> left = multiPurpose(root -> left);
pair<int,int> 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<int,int> 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;
}
157 changes: 157 additions & 0 deletions Lecture065-Trees Questions P3/1A-longestBloodline.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
// { 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;
}


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
Loading