Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * 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 {
- void inordertraversal(TreeNode* root,vector<int> &ans)
- {
- if(root==nullptr)
- {
- return;
- }
- inordertraversal(root->left,ans);
- ans.push_back(root->val);
- inordertraversal(root->right,ans);
- }
- public:
- bool isValidBST(TreeNode* root) {
- vector<int> ans;
- inordertraversal(root,ans);
- for(int i=0;i<ans.size()-1;i++)
- {
- if(ans[i+1]<=ans[i])
- {
- return false;
- }
- }
- return true;
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment