Leetcode 98

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    bool isValidBST(TreeNode* root) {
        vector<int> res;
        int flag = 1;
        long num = LONG_MIN;
        dfs(root,num,flag);
        return flag;
    }
    
    void dfs(TreeNode* root,long& maxnum,int& flag){
        if(root == NULL) return;
        if(root->left) dfs(root->left,maxnum,flag);
        if(root->val > maxnum){
            maxnum = root->val;
        }
        else{
            flag = 0;
        }
        if(root->right) dfs(root->right,maxnum,flag);
    }
};

__卡边界有点恶心,改成LONG就好了

原文地址:https://www.cnblogs.com/cunyusup/p/10582263.html