标签:span i++ val 题目 左右子树 struct max 处理 root
/** * 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) { return isValidBST(root, INT_MIN, INT_MAX); } bool isValidBST(TreeNode *root, int low, int high){ if (root == NULL ) return true; if (low < root->val && root->val < high) return (isValidBST(root->left, low, root->val) && isValidBST(root->right, root->val, high)); else return false; } };
/** * 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) { vector<int> res; isValidBST(root, res); int len = res.size(); bool flag = true; for (int i=0; i<len-1; i++){ if (res[i] >= res[i+1]){ flag = false; break; } } return flag; } void isValidBST(TreeNode *root, vector<int> &res){ if (root == NULL) return; isValidBST(root->left, res); res.push_back(root->val); isValidBST(root->right, res); } };
标签:span i++ val 题目 左右子树 struct max 处理 root
原文地址:http://www.cnblogs.com/Kobe10/p/6357950.html