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. Binder 驱动(三)

    Binder 驱动是 Binder 的最终实现, ServiceManager 和 Client/Service 进程间通信最终都是由 Binder 驱动投递的. Binder 驱动的代码位于 ker ...

  2. mysql Access denied for user root@localhost错误解决方法总结

    原文:http://www.111cn.net/database/mysql/44142.htm Access denied for user 'root'@'localhost' (using pa ...

  3. 添加ASP.NET网站资源文件夹

    ASP.NET应用程序包含7个默认文件夹,分别为Bin.APP_Code.App_GlobalResources.App_LocalResources.App_WebReferences.App_Br ...

  4. 100行代码搞定抖音短视频App,终于可以和美女合唱了。

    欢迎大家前往腾讯云+社区,获取更多腾讯海量技术实践干货哦~ 本文由视频咖 发表于云+社区专栏 本文作者,shengcui,腾讯云高级开发工程师,负责移动客户端开发 最近抖音最近又带了一波合唱的节奏,老 ...

  5. Struts2拦截器和标签

    一.struts2拦截器 1.struts2是框架,封装了很多的功能,struts2里面封装的功能都是在拦截器里面. 2 struts2里面封装了很多的功能,有很多拦截器,不是每次这些拦截器都执行,每 ...

  6. <数据挖掘导论>读书笔记3--分类

    1.分类的基本概念 分类任务就是通过学习得到一个目标函数f,把每个属性集x映射到一个预先定义的类标号y 目标函数也称为分类模型. 2. 解决分类问题的一般方法: 决策树分类法 基于规则的分类法 神经网 ...

  7. Javascript的构造函数和constructor属性

    原型链 function Foo() { this.value = 42;}Foo.prototype = { method: function() {}}; function Bar() {} // ...

  8. 第4天:function对象(案例:获取当前日期属于当年第几天、arguments对象、函数类型、参数、返回值、自身调用)

    获取当前日期输入当年第几天 //输入,年月日,获取这个日期是这一年的第几天 //年-月--日:20171月31日 function getDay(year,month,day){ //定义变量存储对应 ...

  9. 获取路径path

    request 的常用方法 request.getSchema() 返回当前页面使用的协议,http 或是 https; request.getServerName() 返回当前页面所在的服务器的名字 ...

  10. java温故而知新(9)OOP(面向对象编程)理念

    Object   Oriented   Programming   (面向对象的程序设计) 1.定义 面向对象程序设计(OOP)的具体定义很难下,也很容易因此而引起争论,在   Object-Orie ...