/**
* 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) {
return dfs(root, INT_MIN, INT_MAX);
}
bool dfs(TreeNode *root, long long minv, long long maxv){
if(!root) return true;
if(root -> val < minv || root -> val > maxv) return false;
return dfs(root -> left, minv, root -> val -1ll) && dfs(root -> right, root -> val + 1ll, maxv);
}
};
1LL is long long type 1, a number of type int and a long long, the return value is the number of calculation made long long type, this way to prevent overflow.
std :: to_string () is a function of c ++ 11 is introduced, which is used as follows:
string to_string (int val);
string to_string (long val);
string to_string (long long val);
string to_string (unsigned val);
string to_string (unsigned long val);
string to_string (unsigned long long val);
string to_string (float val);
string to_string (double val);
string to_string (long double val);
Sample as follows:
// to_string example
#include <iostream> // std::cout
#include <string> // std::string, std::to_string
int main ()
{
std::string pi = "pi is " + std::to_string(3.1415926);
std::string perfect = std::to_string(1+2+4+7+14) + " is a perfect number";
std::cout << pi << '\n';
std::cout << perfect << '\n';
return 0;
}
Output:
pi is 3.141593 28 is a perfect number
But std :: to_string () can not control the format string is generated, which means that you will encounter such a situation:
std::cout << std::to_string(0.33) << std::endl; 0.330000