二排排序树的判断(中序遍历)
作者:
Lw1
,
2024-09-27 21:40:47
,
所有人可见
,
阅读 3
/**
* 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 f(TreeNode* root , int k)
{
if(root)
{
if(!f(root->left , k)) return false;
if(root->val < k) return false;
k = root->val;
if(!f(root->right , k)) return false;
}
return true;
}
int pathSum(TreeNode* root) {
if( f(root, -0x3f3f3f3f))
cout<<"是二叉排序树"<<endl;
else
cout<<"不是二叉排序树"<<endl;
}
};