Validate Binary Search Tree

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.

confused what "{1,#,2,3}" means? > read more on how binary tree is serialized on OJ.

 
思路一:利用递归的思想,任何一个节点必须要在min和max范围内
 
min和max根据是左子树还是右子树来确定
 
如果是左子树:
node->left<node
 
如果是右子树:
node->right>node
 
 
递归条件:node->val在min和max范围内,node->left要在min,node->val范围内,node->right要在node->val,max范围内
node->val<max&&node->val>min&&testValid(node->left,node->val,min)&&testValid(node->right,max,node->val); 
 
 
 
 /**
* 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) { if(root==NULL)
{
return true;
} if(root->left==NULL&&root->right==NULL)
{
return true;
} //注意如果测试用例含有INT_MAX则,必须采用long long int 才能避免出错
return testValid(root,(long long int)INT_MAX+,(long long int)INT_MIN-);
} bool testValid(TreeNode *node,long long int max, long long int min)
{
if(node==NULL)
{
return true;
} return node->val<max&&node->val>min&&testValid(node->left,node->val,min)&&testValid(node->right,max,node->val); }
};
 
 
 
第二种方法,利用 二叉排序树 中序遍历 结果为递增序列的性质
 
 
 /**
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
vector<int> ret_v;
bool isValidBST(TreeNode *root) { if(root==NULL)
{
return true;
} ret_v.clear();
inOrderTraversal(root); //判断是否递增
for(int i=;i<ret_v.size()-;i++)
{
if(ret_v[i]>=ret_v[i+])
{
return false;
}
}
return true;
} //中序遍历,记录下数值
void inOrderTraversal(TreeNode *root)
{
if(root==NULL)
{
return;
} inOrderTraversal(root->left);
ret_v.push_back(root->val);
inOrderTraversal(root->right);
}
};
 
 
 
第三种:直接在中序遍历中判断:
 
 /**
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public: int ret;
bool flag;
bool isFirstNode; bool isValidBST(TreeNode *root) { flag=true;
//判断是不是第一个被访问的节点
isFirstNode=true; inOrderTraversal(root);
return flag;
} void inOrderTraversal(TreeNode *root)
{
if(root==NULL)
{
return;
} if(!flag)
{
return;
} inOrderTraversal(root->left); //一旦发现不符合升序,则不是二叉排序树
if(!isFirstNode&&ret>=root->val)
{
flag=false;
return;
} ret=root->val;
isFirstNode=false; inOrderTraversal(root->right);
} };

【leetcode】Validate Binary Search Tree的更多相关文章

  1. 【LeetCode】Validate Binary Search Tree ——合法二叉树

    [题目] Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defin ...

  2. 【leetcode】Validate Binary Search Tree(middle)

    Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defined as ...

  3. 【题解】【BST】【Leetcode】Validate Binary Search Tree

    Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defined as ...

  4. 【leetcode】 Validate Binary Search Tree

    Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defined as ...

  5. 【LeetCode】Validate Binary Search Tree 二叉查找树的推断

    题目: Given a binary tree, determine if it is a valid binary search tree (BST). 知识点:BST的特点: 1.一个节点的左子树 ...

  6. 【Leetcode】【Medium】Validate Binary Search Tree

    Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defined as ...

  7. 【LeetCode】二叉查找树 binary search tree(共14题)

    链接:https://leetcode.com/tag/binary-search-tree/ [220]Contains Duplicate III (2019年4月20日) (好题) Given ...

  8. 【LeetCode】173. Binary Search Tree Iterator 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 保存全部节点 只保留左节点 日期 题目地址:http ...

  9. 【leetcode】Recover Binary Search Tree

    Recover Binary Search Tree Two elements of a binary search tree (BST) are swapped by mistake. Recove ...

随机推荐

  1. "Java 反序列化"过程远程命令执行漏洞

    一.漏洞描述   国外 FoxGlove 安全研究团队于2015年11月06日在其博客上公开了一篇关于常见 Java 应用如何利用反序列化操作进行远程命令执行的文章.原博文所提到的 Java 应用都使 ...

  2. Java Lambda表达式入门

    Java Lambda表达式入门 http://blog.csdn.net/renfufei/article/details/24600507 Java 8十个lambda表达式案例 http://w ...

  3. 基本linux命令

    1.mkdir mkdir 创建目录   mkdir -p 循环创建目录 2.cd     切换目录 3.pwd   查看当前路径 4.mkdir 删除一个空的目录 5.cp 复制文件/目录  -r用 ...

  4. Object&&String学习

    Object类 列表项 String类 常用方法 构造方法 public String() public String(byte[] bytes) public String(byte[]bytes, ...

  5. sqoop

    http://blog.csdn.net/yfkiss/article/details/8700480 http://www.cnblogs.com/admln/p/sqoop1-99-4-javaa ...

  6. WCF :IIS寄宿方式的Web地址、BaseAddress和EndPoint Address的关系

    对于在IIS中通过W3SVC或WAS寄宿的WCF Service,其在浏览器中显示的地址(Web地址),与其配置文件中的BaseAddress和EndPoint Address有什么关系呢?让我们来分 ...

  7. IBM:领导力素质的三环模式

    文章来源:http://blog.vsharing.com/sdtcj/A1826934.html   在翰威特公司于2003年评选的美国企业“领导人才最佳雇主”名单上,IBM名列榜首.而纽约< ...

  8. UESTC 1852 Traveling Cellsperson

    找规律水题... Traveling Cellsperson Time Limit: 1000ms Memory Limit: 65535KB This problem will be judged ...

  9. Spring配置bean文件的底层实现方式

    首先 bean文件如下: <beans> <bean id="date" class="java.util.Date"></bea ...

  10. Linux 信号量大全

    编号 信号名称 缺省动作 说明 1 SIGHUP 终止 终止控制终端或进程 2 SIGINT 终止 键盘产生的中断(Ctrl-C) 3 SIGQUIT dump 键盘产生的退出 4 SIGILL du ...