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 a Two Sum Program and Validate BST using cpp language #7355

Closed
wants to merge 3 commits into from
Closed
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
38 changes: 38 additions & 0 deletions Program's_Contributed_By_Contributors/Cpp_Program/Two_Sum.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#include <iostream>
#include <sstream> // For stringstream to convert string to number
#include <string>
using namespace std;

// Function to convert string input to a double and add two numbers
double convertAndAdd(string input1, string input2)
{
double num1, num2;

// Convert the first input to double
stringstream ss1(input1);
ss1 >> num1;

// Convert the second input to double
stringstream ss2(input2);
ss2 >> num2;

// Return the sum
return num1 + num2;
}

int main()
{
string input1, input2;

// Taking two numbers as input from the user
cout << "Enter the first number: ";
cin >> input1;
cout << "Enter the second number: ";
cin >> input2;

// Add the numbers and display the result
double sum = convertAndAdd(input1, input2);
cout << "The sum of the two numbers is: " << sum << endl;

return 0;
}
43 changes: 43 additions & 0 deletions Program's_Contributed_By_Contributors/Cpp_Program/ValidateBST
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#include<bits/stdc++.h>
using namespace std;

struct Node {
int data;
Node* left;
Node* right;
Node(int val) {
data = val;
left = right = NULL;
}
};

bool ValidateBSTChecker(Node* node, int min_val, int max_val) {
if (node == NULL)
return true;

if (node->data < min_val || node->data > max_val)
return false;

return ValidateBSTChecker(node->left, min_val, node->data - 1) &&
ValidateBSTChecker(node->right, node->data + 1, max_val);
}

bool ValidateBST(Node* root) {
return ValidateBSTChecker(root, INT_MIN, INT_MAX);
}

int main() {
// Example usage
Node* root = new Node(4);
root->left = new Node(2);
root->right = new Node(5);
root->left->left = new Node(1);
root->left->right = new Node(3);

if (ValidateBST(root))
cout << "The tree is a valid BST.";
else
cout << "The tree is not a valid BST.";

return 0;
}
Loading