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.

解题思路1:

1、中序遍历二叉树,并将遍历结果装进数组。

2、检查数组是否由低到高依次排列。

注意:如果两个结点值一样,也判定为false;

代码:

 /**
* 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 isValidBST(TreeNode* root) {
vector<int> nums;
stack<TreeNode*> nodes;
TreeNode* curNode = root; while (curNode || !nodes.empty()) {
while (curNode) {
nodes.push(curNode);
curNode = curNode->left;
} curNode = nodes.top();
nodes.pop();
nums.push_back(curNode->val);
curNode = curNode->right;
} for (int i = ; i < nums.size(); ++i) {
if (nums[i] <= nums[i-])
return false;
} return true;
}
};

解题思路2:

判断一个二叉树是不是二叉搜索树,除了判断是否满足 “左侧子树所有结点 < 当前结点 < 右侧子树所有结点”的方式外;

还可以判断是否满足:

1、子树的最左结点(最小结点),大于子树的“左父亲”;

2、子树中,每个结点大于自己的左儿子;

(“左父亲”:结点是自己的右子树的左父亲)

使用中序遍历的方式,判断一个子树是否满足二叉查找树:

0、遍历之前记录子树的“左父亲”的值;

1、判断左子树是否满足二叉查找树;

2、如果此结点没有左孩子,则判断此结点值,是否大于整个子树的“左父亲”(如果树没有“左父亲”,即没有父亲或者是父亲的左子树,则跳过此步);

3、如果此结点有左孩子,则判断此结点值,是否大于自己的左孩子;

4、将这个点作为“左父亲”,检查此结点的右子树是否是二叉查找树;

代码实现:

新建一个left指针,用于保存子树的“左父亲”或者结点左儿子;

如果当前结点是整个子树的最左结点,则left保存的是“左父亲”,结点需要>“左父亲”;

如果当前结点有左孩子,则left保存的是“左孩子”,结点需要>“左孩子”;

代码:

(注意left必须是地址形式在函数内可改,否则从左子树判定返回时,不能记录当前结点左儿子的数据)

 /**
* 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 isValidBST(TreeNode* root) {
TreeNode* left = NULL;
return validate(root, left);
} bool validate(TreeNode* node, TreeNode* &left) {
if (node == NULL)
return true;
if (validate(node->left, left) == false)
return false;
if (left != NULL && left->val >= node->val)
return false;
left = node;
return validate(node->right, left);
}
};

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

  1. 【leetcode】Validate Binary Search Tree

    Validate Binary Search Tree Given a binary tree, determine if it is a valid binary search tree (BST) ...

  2. 【LeetCode练习题】Validate Binary Search Tree

    Validate Binary Search Tree Given a binary tree, determine if it is a valid binary search tree (BST) ...

  3. 【LeetCode】98. Validate Binary Search Tree (2 solutions)

    Validate Binary Search Tree Given a binary tree, determine if it is a valid binary search tree (BST) ...

  4. Leetcode 笔记 98 - Validate Binary Search Tree

    题目链接:Validate Binary Search Tree | LeetCode OJ Given a binary tree, determine if it is a valid binar ...

  5. leetcode dfs Validate Binary Search Tree

    Validate Binary Search Tree Total Accepted: 23828 Total Submissions: 91943My Submissions Given a bin ...

  6. LeetCode: Validate Binary Search Tree 解题报告

    Validate Binary Search Tree Given a binary tree, determine if it is a valid binary search tree (BST) ...

  7. [LeetCode] Lowest Common Ancestor of a Binary Search Tree 二叉搜索树的最小共同父节点

    Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BS ...

  8. 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 ...

  9. [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 ...

  10. [LeetCode] 255. Verify Preorder Sequence in Binary Search Tree 验证二叉搜索树的先序序列

    Given an array of numbers, verify whether it is the correct preorder traversal sequence of a binary ...

随机推荐

  1. apache2 + django

    参照: http://blog.topspeedsnail.com/archives/7828 注意,当django安装在虚拟环境下时,配置文件里面需要有python-home 指向虚拟环境 WSGI ...

  2. linux(ubuntu-16.1) 下安装 odoo10 新版

    1.虚拟机(VMware)中安装 ubuntu-16.1(网络适配器选择桥接模式). 安装成功后,运行 ubuntu 提示 "CPU已被客户机操作系统禁用" 时,需要修改配置文件解 ...

  3. (转)MySQL性能调优my.cnf详解

    MySQL性能调优my.cnf详解 https://blog.linuxeye.cn/379.html http://blog.csdn.net/orichisonic/article/details ...

  4. selenium+python(数据驱动测试)

    自动化领域的两种驱动,对象驱动与数据驱动 数据驱动:测试数据的改变引起执行结果的改变 叫 数据驱动 关键字驱动:测试对象名字的改变起引起测试结果的改变 叫 关键字驱动 1 .读取文件参数化   以百度 ...

  5. 【Lua】CJSON的安装

    Lua CJSON 是 Lua 语言提供高性能的 JSON 解析器和编码器,其性能比纯 Lua 库要高 10 到 20 倍.Lua CJSON 完全支持 UTF-8 ,无需依赖其他非 Lua/LuaJ ...

  6. JVM的内存结构

    程序计数器 程序计数器(Program Counter Register)是一块较小的内存空间,它可以看作是当前线程所执行的字节码的行号指示器.字节码解释器工作时就是通过改变这个计数器的值来选取下一条 ...

  7. vue项目中总结用到的方法。

    依赖 vue-router 获得当前字符串,对应当前路由的路径,总是解析为绝对路径. computed: { productIcon () { return this.imgMap[this.$rou ...

  8. Asp.Net MVC4通过id更新表单

    用户需求是:一个表单一旦创建完,其中大部分的字段便不可再编辑.只能编辑其中部分字段. 而不可编辑是通过对input输入框设置disabled属性实现的,那么这时候直接向数据库中submit表单中的内容 ...

  9. Nodejs 8.0 踩坑经验汇总

    .Linq:Linq to sql 类 高度集成化的数据库访问技术 使用Linq是应该注意的问题: 1.创建Linq连接后生成的dbml文件不要变动,生成的表不要碰,拖动表也会造成数据库连接发生变动, ...

  10. 简单的CRUD(一)

    一.JDBC的概述--(来源于百度) JDBC(Java DataBase Connectivity,java数据库连接)是一种用于执行SQL语句的Java API,可以为多种关系数据库提供统一访问, ...