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. 【.Net】 C#参数数组与函数重载

    static int ParamsFunc(int i, string s) { return i; } static int ParamsFunc(int i, string s, params i ...

  2. redis虚拟内存---官方文档

    http://redis.io/topics/internals-vm Virtual Memory technical specification This document details the ...

  3. HDU 5698——瞬间移动——————【逆元求组合数】

    瞬间移动 Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submis ...

  4. HDU 5656 ——CA Loves GCD——————【dp】

    CA Loves GCD Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)To ...

  5. Go语言备忘录(3):net/http包的使用模式和源码解析

    本文是晚辈对net/http包的一点浅显的理解,文中如有错误的地方请前辈们指出,以免误导! 转摘本文也请注明出处:Go语言备忘录(3):net/http包的使用模式和源码解析,多谢!  目录: 一.h ...

  6. php根据IP获取所在省份-百度api接口

    这里用的file_put_contents,你也可以用别的,直接怼代码: //拼接传递的参数 $getData = array( 'query' => '127.0.0.1', 'resourc ...

  7. pdf OCR

    pdf转word等其他可排版编辑格式的软件: ABBYY Finereader: 老牌OCR软件了,支持各种文字.图片.表格的识别,效率比较高,中文的识别效果也很好,公式的转换效率较差. InftyR ...

  8. Lucene学习之四:Lucene的索引文件格式(2)

    本文转载自:http://www.cnblogs.com/forfuture1978/archive/2009/12/14/1623599.html  略有删减和补充 四.具体格式 上面曾经交代过,L ...

  9. ASP.NET MVC4 新手入门教程之四 ---4.添加一个模型

    在本节中,您将添加一些类,用于管理数据库中的电影.这些类将 ASP.NET MVC 应用程序的"模型"部分. 您将使用一种称为实体框架的.NET 框架数据接入技术来定义和使用这些模 ...

  10. 文档类型DTD,DOCTYPE和浏览器模式

    出处:http://blog.csdn.net/freshlover/article/details/11616563 浏览器从服务端获取网页后会根据文档的DOCTYPE定义显示网页,如果文档正确定义 ...