From a3cbbc2bb7ad5300378a3c852701928e26aa36a4 Mon Sep 17 00:00:00 2001 From: Sayahnneeta Dutta Date: Thu, 27 Oct 2022 02:12:19 +0530 Subject: [PATCH 1/2] valid binary tree in cpp --- Medium/ValidBinaryTree.cpp | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 Medium/ValidBinaryTree.cpp diff --git a/Medium/ValidBinaryTree.cpp b/Medium/ValidBinaryTree.cpp new file mode 100644 index 0000000..d4786ea --- /dev/null +++ b/Medium/ValidBinaryTree.cpp @@ -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); + + } + +}; \ No newline at end of file From d38021c0d1c9a40e5b9230af7f01055bfe24aab7 Mon Sep 17 00:00:00 2001 From: Sayahnneeta Dutta Date: Thu, 27 Oct 2022 02:18:32 +0530 Subject: [PATCH 2/2] Next greater element in cpp --- Easy/NextGreaterElement.cpp | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 Easy/NextGreaterElement.cpp diff --git a/Easy/NextGreaterElement.cpp b/Easy/NextGreaterElement.cpp new file mode 100644 index 0000000..f559787 --- /dev/null +++ b/Easy/NextGreaterElement.cpp @@ -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 +using namespace std +class Solution { +public: + vector nextGreaterElement(vector& nums1, vector& nums2) { + vector ans; + + for(int i =0; i