Validate Binary Search Tree(DFS)
Given a binary tree, determine if it is a valid binary search tree (BST).
Assume a BST is defined as follows:
- The left subtree of a node contains only nodes with keys less than the node's key.
- The right subtree of a node contains only nodes with keys greater than the node's key.
- Both the left and right subtrees must also be binary search trees.
描述:即判断一棵树是不是二叉搜索树(左孩子小于父节点值,右孩子大于父亲节点值,且其左子树和右子树也同时满足BST)
思路:双层递归。
第一层,递归每一个节点是否满足,左边所有节点都小于它,右边所有节点都大于它。
第二层,如何来实现判断,左边所有节点都小于根,右边所有节点大于根。(每个节点都要作为根)。
特殊:root为空时,return true
代码:
class Solution {
public:
bool isLeftValid(TreeNode *root,int val){//某个根的左树,val该根的值
if(root==NULL)
return true;
return root->val<val&&isLeftValid(root->left,val)&&isLeftValid(root->right,val);
}
bool isRightValid(TreeNode *root,int val){//某个根的右树,val该根的值
if(root==NULL)
return true;
return root->val>val&&isRightValid(root->left,val)&&isRightValid(root->right,val);
}
bool isValidBST(TreeNode *root) {
if(root==NULL)
return true;
bool flag=isLeftValid(root->left,root->val)&&isRightValid(root->right,root->val);
if(!flag)
return false;
return isValidBST(root->left)&&isValidBST(root->right);
}
};
Validate Binary Search Tree(DFS)的更多相关文章
- leetcode dfs Validate Binary Search Tree
Validate Binary Search Tree Total Accepted: 23828 Total Submissions: 91943My Submissions Given a bin ...
- LeetCode: Validate Binary Search Tree 解题报告
Validate Binary Search Tree Given a binary tree, determine if it is a valid binary search tree (BST) ...
- Leetcode 笔记 98 - Validate Binary Search Tree
题目链接:Validate Binary Search Tree | LeetCode OJ Given a binary tree, determine if it is a valid binar ...
- Validate Binary Search Tree
Validate Binary Search Tree Given a binary tree, determine if it is a valid binary search tree (BST) ...
- 【leetcode】Validate Binary Search Tree
Validate Binary Search Tree Given a binary tree, determine if it is a valid binary search tree (BST) ...
- LintCode Validate Binary Search Tree
Validate Binary Search Tree Given a binary tree, determine if it is a valid binary search tree (BST) ...
- 39. Recover Binary Search Tree && Validate Binary Search Tree
Recover Binary Search Tree OJ: https://oj.leetcode.com/problems/recover-binary-search-tree/ Two elem ...
- [CareerCup] 4.5 Validate Binary Search Tree 验证二叉搜索树
4.5 Implement a function to check if a binary tree is a binary search tree. LeetCode上的原题,请参见我之前的博客Va ...
- 【LeetCode练习题】Validate Binary Search Tree
Validate Binary Search Tree Given a binary tree, determine if it is a valid binary search tree (BST) ...
随机推荐
- java获取公网ip以及物理地址和代理商
package ipconfig; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStr ...
- 001原始编译全志r6平台tinav3.0.2系统
001原始编译全志r6平台tinav3.0.2系统 2018/6/8 11:32 版本:V1.0 开发板:R6 SDK:tina v3.0.2 1.01原始编译全志r16平台tinav3.0系统: r ...
- spark源码编译,运行example遇到:NoClassDefFoundError: org/spark_project/guava/cache/CacheLoader
基本环境: win10+idea Scala2.11.8 maven3.5.3 spark2.1.0 问题: 在window10下编译spark2.1.0源码,在idea下运行example,遇到问题 ...
- 修改xampp的mysql默认密码和端口
修改MySQL默认密码 MySQL 的“root”用户默认状态是没有密码的,所以在 PHP 中您可以使用 mysql_connect("localhost","root& ...
- java-IO操作性能对比
在软件系统中,IO速度比内存速度慢,IO读写在很多情况下会是系统的瓶颈. 在java标准IO操作中,InputStream和OutputStream提供基于流的IO操作,以字节为处理单位:Reader ...
- 使用Win7 64位旗舰版光盘映像安装Windows Home basic 64位操作系统
工作当中需要安装Windows home basic 64位操作系统,苦于手头没有该版本的安装光盘,也没时间下载其安装映像.因此,在现有资源“cn_windows_7_ultimate_with_sp ...
- druid数据库连接池整合到SpringMvc
1.maven项目加入相关的依赖 <dependency> <groupId>com.alibaba</groupId> <artifactId>dru ...
- Python_练习_VS清理器
#导入os import os #创建列表放入后缀 d=[ '.txt','obj','tlog','lastbuildstate','idb','pdb','pch','res','ilk','sd ...
- 【东软实训】SQL函数
SQL函数 SQL是用于访问和处理数据库的标准的计算机语言,我们所使用的的是Oracle SQL 一个数据库通常包含一个或多个表,每个表有一个名字表示,下图即为一个名为“emp”的表,接下来的操作都将 ...
- 雷林鹏分享:PHP Secure E-mails
在上一节中的 PHP e-mail 脚本中,存在着一个漏洞. PHP E-mail 注入 首先,请看上一章中的 PHP 代码: if (isset($_REQUEST['email'])) { // ...