一. 题目描写叙述

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?

二. 题目分析

这道题的大意是,推断一个二叉查找树是否合法的,这个能够依据二叉查找树的定义来进行推断,即一个内部结点的值要大于左子树的最大值,同一时候要小于右子树的最大值。

依据这个定义。能够递归得进行推断,这样的方法得时间复杂度为O(n),空间复杂度为O(logn)。这道题要注意的地方就是要记得更新以结点为父节点的树的最大值和最小值。以便递归返回时给上一个调用进行推断。

三. 演示样例代码

#include <iostream>

struct TreeNode
{
int val;
TreeNode *left;
TreeNode *right;
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
}; class Solution
{
private:
bool isValidBST(TreeNode* root, int &MinValue, int &MaxValue)
{
if (!root)
{
return true;
} if (root->left)
{
if (root->val <= root->left->val)
{
return false;
} int LeftMinValue = 0;
int LeftMaxValue = 0;
if (!isValidBST(root->left, LeftMinValue, LeftMaxValue))
{
return false;
}
else
{
MinValue = LeftMinValue;
if (LeftMaxValue != LeftMinValue)
{
if (root->val <= LeftMaxValue)
{
return false;
}
}
}
}
else
{
MinValue = root->val;
} if (root->right)
{
if (root->val >= root->right->val)
{
return false;
} int RightMinValue = 0;
int RightMaxValue = 0; if (!isValidBST(root->right, RightMinValue, RightMaxValue))
{
return false;
}
else
{
MaxValue = RightMaxValue;
if (RightMaxValue != RightMinValue)
{
if (root->val >= RightMinValue)
{
return false;
}
}
}
}
else
{
MaxValue = root->val;
} return true;
} public:
bool isValidBST(TreeNode* root)
{
int MinValue = 0;
int MaxValue = 0;
bool IsLeaf = true; return isValidBST(root, MinValue, MaxValue);
}
};

leetcode笔记: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 dfs Validate Binary Search Tree

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

  3. Java for LeetCode 098 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] 98. 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 98. Validate Binary Search Tree

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

  6. 【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 ...

  7. 【题解】【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 ...

  8. leetcode 98 Validate Binary Search Tree ----- java

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

  9. [leetcode]98. Validate Binary Search Tree验证二叉搜索树

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

  10. 【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 ...

随机推荐

  1. Java获取当前时间的年月日方法

    package com.ob; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util ...

  2. SpringMVC MongoDB之“基本文档查询(Query、BasicQuery)”

    一.简介 spring Data  MongoDB提供了org.springframework.data.mongodb.core.MongoTemplate对MongoDB的CRUD的操作,上一篇我 ...

  3. .Net开发工程师工具箱

    Visual Studio Visual Studio Productivity Power tool:Visual Studio专业版(及以上)的扩展,具有丰富的功能,如快速查找,导航解决方案,可搜 ...

  4. [转] React 是什么

    用脚本进行DOM操作的代价很昂贵.有个贴切的比喻,把DOM和JavaScript各自想象为一个岛屿,它们之间用收费桥梁连接,js每次访问DOM,都要途径这座桥,并交纳“过桥费”,访问DOM的次数越多, ...

  5. python全栈开发day49-jquery的位置信息、事件流、事件对象,事件委托,事件绑定和解绑

    一.昨日内容回顾    1. jQuery的属性操作 1) html属性操作:attr 2) DOM属性操作:prop 3) 类样式操作:addClass.removeClass.toggleClas ...

  6. User Agent 设置

    感谢版主回复,版主贴的方法网上到处都是,我试了很多次都是不行的,有用的方法都几乎这个到处转贴的信息淹没了. 今天我总算在一个博客找了到可行的方法,转过来和大家分享 Windows Registry E ...

  7. BZOJ1297 [SCOI2009]迷路 矩阵乘法

    欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - BZOJ1297 题意概括 有向图有 N 个节点,从节点 0 出发,他必须恰好在 T 时刻到达节点 N-1. ...

  8. 【转】TCP三次握手和四次挥手全过程及为什么要三次握手解答

    TCP三次握手和四次挥手的全过程   TCP是主机对主机层的传输控制协议,提供可靠的连接服务,采用三次握手确认建立一个连接: 位码即tcp标志位,有6种表示: SYN(synchronous建立连接) ...

  9. 洛谷 P1387 最大正方形 【dp】(经典)

    题目链接:https://www.luogu.org/problemnew/show/P1387 题目描述 在一个n*m的只包含0和1的矩阵里找出一个不包含0的最大正方形,输出边长. 输入格式: 输入 ...

  10. HBase 开发环境搭建(Eclipse\MyEclipse + Maven)

    写在前面的话 首先, 搭建基于MyEclipse的Hadoop开发环境 相信,能看此博客的朋友,想必是有一定基础的了.我前期写了大量的基础性博文.可以去补下基础. 比如, CentOS图形界面下如何安 ...