一. 题目描写叙述

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. python+selenium十二:一个输入框双层input标签

    先点击第一个,再对第二个进行操作,否则操作失败 driver.find_element_by_css_selector(".pwd").click()driver.find_ele ...

  2. Crack相关

    Microsoft Office 2007专业增强版密钥:KXFDR-7PTMK-YKYHD-C8FWV-BBPVWM7YXX-XJ8YH-WY349-4HPR9-4JBYJCTKXX-M97FT-8 ...

  3. 交叉编译和安装ARM板(RK3288)和Linux 3.10上的RTL8188无线网卡驱动

    插入无线网卡,输入ifconfig,发现没有检测到网卡. 输入lsusb,查看无线网卡型号. 我用的无线网卡是EDUP的网卡,包装盒里有一张驱动光盘,把光盘里linux下的驱动目录复制下来.如果没有驱 ...

  4. Linux下安装JDK7和TomCat7

    [BEGIN] 2016/9/9 14:20:49[root@rzhd jdk]# ll总用量 149916-rw-r--r-- 1 root root 153512879 9月 9 14:20 jd ...

  5. CSS3滚动条美化,CSS3滚动条皮肤

    CSS3 -webkit-scrollbar滚动条皮肤美化实现,利用-webkit-scrollbar,-webkit-scrollbar-track,-webkit-scrollbar-thumb这 ...

  6. BZOJ1787 [Ahoi2008]Meet 紧急集合 LCA

    欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - BZOJ1787 题意概括 有一棵节点为n个(n≤500000)的树.接下来m次询问(m≤500000),每次 ...

  7. C#连接数据库MD5数据库加密

    创建StringHelper类 首先数据库里的资料是加密了的. 创建将指定的字符串加密为MD5密文方法 public static string ToMD5(string source){ Strin ...

  8. rocketMQ基本架构简介

    1.RocketMQ 简介: RocketMQ前身是阿里研发的一个队列模型的消息中间件,后开源给apache基金会成为了apache的顶级开源项目,具有高性能.高可靠.高实时.分布式特点. 2.Roc ...

  9. Tkinter的下拉列表Combobox

    Tkinter的下拉列表Combobox   tk中下拉列表使用ttk.Combobox,代码如下:   #!/usr/bin/env python   # -*- coding: utf-8 -*- ...

  10. 洛谷 P1464 Function【记忆化搜索】

    题目链接 题目描述 对于一个递归函数w(a,b,c) 如果a<=0 or b<=0 or c<=0就返回值1. 如果a>20 or b>20 or c>20就返回w ...