Skip to content
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
176 changes: 176 additions & 0 deletions 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
30 changes: 30 additions & 0 deletions reverseString.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#include<iostream>
using namespace std;

void reverse(string& str, int i, int j ) {

cout << "call recieved for " << str << endl;

//base case
if(i>j)
return ;

swap(str[i], str[j]);
i++;
j--;

//Recursive call
reverse(str,i,j);

}

int main() {

string name = "abcde";
cout << endl;
reverse(name, 0 , name.length()-1 );
cout << endl;
cout << name << endl;

return 0;
}