标签:val less ali nta input sum xpl content css
Assume a BST is defined as follows:
Example 1: 2 / 1 3 Input: [2,1,3] Output: true Example 2: 5 / 1 4 / 3 6 Input: [5,1,4,null,null,3,6] Output: false Explanation: The root node‘s value is 5 but its right child‘s value is 4.
code
struct TreeNode { int val; TreeNode *left; TreeNode *right; TreeNode(int x) : val(x), left(NULL), right(NULL) {} }; class Solution { public: bool isValidBST(TreeNode* root) { if(!root) return true; return isValidTreeBSTCore(root,nullptr,nullptr); } private: bool isValidTreeBSTCore(TreeNode *root,TreeNode *left,TreeNode *right) { if(root==nullptr) return true; if(left&&left->val>=root->val) return false; if(right&&right->val<=root->val) return false; if(isValidTreeBSTCore(root->left,left,root)&&isValidTreeBSTCore(root->right,root,right)) return true; return false; } };
标签:val less ali nta input sum xpl content css
原文地址:https://www.cnblogs.com/tianzeng/p/10954336.html