diff --git a/bloodline.cpp b/bloodline.cpp new file mode 100644 index 0000000..5c5e834 --- /dev/null +++ b/bloodline.cpp @@ -0,0 +1,176 @@ +// { 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; +} + + + // } 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 \ No newline at end of file diff --git a/reverseString.cpp b/reverseString.cpp new file mode 100644 index 0000000..1821103 --- /dev/null +++ b/reverseString.cpp @@ -0,0 +1,30 @@ +#include +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; +} \ No newline at end of file