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

valid binary tree in cpp #303

Open
wants to merge 2 commits 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
31 changes: 31 additions & 0 deletions Easy/NextGreaterElement.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
//he next greater element of some element x in an array is the first greater element that is to the right of x in the same array

#include <bits.stdc++.h>
using namespace std
class Solution {
public:
vector<int> nextGreaterElement(vector<int>& nums1, vector<int>& nums2) {
vector<int> ans;

for(int i =0; i<nums1.size(); i++){
int j =0;

while(nums1[i]!= nums2[j])
j++;

int c =0;
for(int k = j; k<nums2.size(); k++){
if(nums2[j] < nums2[k]){
ans.push_back(nums2[k]);
c++;
break;
}
}

if(c ==0)
ans.push_back(-1);
}
return ans;

}
};
25 changes: 25 additions & 0 deletions Medium/ValidBinaryTree.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
//Given the root of a binary tree, determine if it is a valid binary search tree (BST).

// Definition for a binary tree node.
struct TreeNode {
int val;
TreeNode *left;
TreeNode *right;
TreeNode() : val(0), left(nullptr), right(nullptr) {}
TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
};
class Solution {
public:
bool helper(TreeNode*root,long min,long max){
if(root==NULL)return true;
if(root->val<=min || root->val>=max)return false;
return helper(root->left,min,root->val)&&helper(root->right,root->val,max);

}
bool isValidBST(TreeNode* root) {
return helper(root,LONG_MIN,LONG_MAX);

}

};