首页 > 其他分享 >98. 验证二叉搜索树c

98. 验证二叉搜索树c

时间:2024-03-06 14:14:48浏览次数:19  
标签:pre return struct 验证 二叉 98 bool TreeNode root

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     struct TreeNode *left;
 *     struct TreeNode *right;
 * };
 */
 
bool judge(struct TreeNode* root,long* pre){
    if(!root) return true;
    bool a=judge(root->left,pre);
    if(root->val <= *pre) return false;
    *pre=root->val;
    bool b=judge(root->right,pre);

    return a&&b;
}


bool isValidBST(struct TreeNode* root) {
    if(!root) return true;
    if(!root->left&&!root->right ) return true;
    long pre=LONG_MIN;
    return judge(root,&pre);
}

结果:

标签:pre,return,struct,验证,二叉,98,bool,TreeNode,root
From: https://www.cnblogs.com/llllmz/p/18056397

相关文章

  • 700. 二叉搜索树中的搜索c
    /***Definitionforabinarytreenode.*structTreeNode{*intval;*structTreeNode*left;*structTreeNode*right;*};*/structTreeNode*searchBST(structTreeNode*root,intval){if(!root)returnNULL;while(root){......
  • 617. 合并二叉树 c
    /***Definitionforabinarytreenode.*structTreeNode{*intval;*structTreeNode*left;*structTreeNode*right;*};*/structTreeNode*mergeTrees(structTreeNode*root1,structTreeNode*root2){if(!root1&&!roo......
  • 106. 从中序与后序遍历序列构造二叉树 c
    /***Definitionforabinarytreenode.*structTreeNode{*intval;*structTreeNode*left;*structTreeNode*right;*};*/structTreeNode*buidl_tree(int*inorder,inthead1,intn1,int*postorder,inthead2,intn2){if(n1<......
  • 654. 最大二叉树c
    /***Definitionforabinarytreenode.*structTreeNode{*intval;*structTreeNode*left;*structTreeNode*right;*};*/intmaxindex(int*nums,inthead,inttail){if(head==tail)returnhead;intmax=head;for(int......
  • 【C++】判断一颗二叉树是否对称
    四步法:(1)如果两个子树都为空指针,则它们相等或对称(2)如果两个子树只有一个为空指针,则它们不相等或不对称(3)如果两个子树根节点的值不相等,则它们不相等或不对称(4)根据相等或对称要求,进行递归处理。//四步法判断一颗二叉树是否对称//主函数boolisSymmetric(TreeNode*root){......
  • 【C++】二叉树的前序、中序、后序遍历(递归、非递归)
    #include<vector>#include<iostream>#include<string>usingnamespacestd;//二叉树的定义structTreeNode{intval;TreeNode*left;TreeNode*right;TreeNode(inta):val(a),left(NULL),right(NULL){}};//使用递归进行前序遍历voidpreoder(Tr......
  • 【C++】求二叉树的最大深度和最小深度
    //求一颗二叉树的最大深度求高度:后序遍历求深度:前序遍历intmaxDepth(TreeNode*root){returnroot?1+max(maxDepth(root->left),maxDepth(root->right)):0;}//求一颗二叉树的最小深度(实质上是后序遍历)intminDepth(TreeNode*root){if(!root)retur......
  • 【C++】翻转二叉树(递归、非递归)
    //使用递归翻转二叉树TreeNode*reverseTree(TreeNode*root){if(!root)returnroot;swap(root->left,root->right);reverseTree(root->left);reverseTree(root->right);returnroot;}//使用队列翻转二叉树层序遍历TreeNode*invertTree(TreeNode*root)......
  • 【教程】无法验证app需要互联网连接以验证是否信任开发者
    摘要本文将探讨在使用苹果App时遇到无法验证开发者的情况,以及用户可以采取的解决方案。通过检查网络连接、重新操作、验证描述文件等方式来解决无法验证开发者的问题。同时,还介绍了开发者信任设置的步骤,以及使用appuploader工具进行安装测试的方法。引言在使用苹果App时,有时会......
  • 代码随想录算法训练营第三十七天 | 738. 单调递增的数字,968.监控二叉树
    968.监控二叉树 已解答困难 相关标签相关企业 给定一个二叉树,我们在树的节点上安装摄像头。节点上的每个摄影头都可以监视其父对象、自身及其直接子对象。计算监控树的所有节点所需的最小摄像头数量。 示例1:输入:[0,0,null,0,0]输出:1......