leetcode: Validate Binary Search Tree

来源:互联网 发布:sql数据库查询器 编辑:程序博客网 时间:2024/06/08 06:39
/** * Definition for binary tree * 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 == NULL)            return true;        return isValidNode( root->left, root->val, INT_MIN) && isValidNode( root->right, INT_MAX, root->val);    }    bool isValidNode( TreeNode *root, int max, int min){        if( root == NULL)            return true;        if( root->val < max && root->val > min)            return isValidNode( root->left, root->val, min) && isValidNode( root->right, max, root->val);        else            return false;    }};

0 0
原创粉丝点击