惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

宝玉的分享
宝玉的分享
NISL@THU
NISL@THU
E
Exploit-DB.com RSS Feed
L
LINUX DO - 热门话题
L
Lohrmann on Cybersecurity
K
Kaspersky official blog
Project Zero
Project Zero
Cisco Talos Blog
Cisco Talos Blog
T
The Exploit Database - CXSecurity.com
P
Palo Alto Networks Blog
C
CXSECURITY Database RSS Feed - CXSecurity.com
T
Threatpost
S
Schneier on Security
G
GRAHAM CLULEY
The Hacker News
The Hacker News
T
Threat Research - Cisco Blogs
Scott Helme
Scott Helme
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
P
Privacy & Cybersecurity Law Blog
C
Cyber Attacks, Cyber Crime and Cyber Security
Cyberwarzone
Cyberwarzone
C
CERT Recently Published Vulnerability Notes
T
Tor Project blog
AWS News Blog
AWS News Blog
Simon Willison's Weblog
Simon Willison's Weblog
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
爱范儿
爱范儿
P
Privacy International News Feed
云风的 BLOG
云风的 BLOG
P
Proofpoint News Feed
S
Securelist
G
Google Developers Blog
The Last Watchdog
The Last Watchdog
Google Online Security Blog
Google Online Security Blog
美团技术团队
F
Fortinet All Blogs
小众软件
小众软件
Recorded Future
Recorded Future
V
Visual Studio Blog
B
Blog RSS Feed
H
Help Net Security
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
Google DeepMind News
Google DeepMind News
Blog — PlanetScale
Blog — PlanetScale
博客园 - 聂微东
Stack Overflow Blog
Stack Overflow Blog
Martin Fowler
Martin Fowler
Latest news
Latest news
Spread Privacy
Spread Privacy
H
Heimdal Security Blog

博客园 - 新一

HPUX 大文件系统扩容 LESS详解之函数(四) Win7 安装Apache 2.2.4报错:<OS 5>拒绝访问. :Failed to open the WinNT service manager java后台进程和线程优先级 在Linux下用源码编译安装apache2 Java IO--压缩流 线程池技术 Red Hat dhclient [置顶] 开关电源的pcb设计规范 javascript 的一些理解和随笔 动态规划之 <筷子> 都是权限惹的祸! - 新一 解决IE6-IE8 Js代码不执行问题 linux下chmod使用 熬之滴水成石:最想深入了解的内容--windows内核机制(6) linux网络设备—PHY linux网络设备—mdio总线 通过action传过来的值在option获取进行验证 屌丝也用按位与(&),按位或(|) (二)
[LeetCode] Validate Binary Search Tree
新一 · 2013-11-14 · via 博客园 - 新一

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)左子树的最大值小于当前节点的键值;

(2)右子树的最小值大于当前节点的键值;

(3)左右子树都是二叉查找树。

当然,在判断完了之后,还应该返回本二叉树的最小值和最大值。

代码将情况分成了5中情况:

(1)节点是空节点,只在树本身就是空的情况下;

(2)左右子树都为空,节点是叶子节点,此时它是二叉查找树,并且该树的最小值和最大值都是该节点的值;

(3)左子树为空,右子树不空,此时只要右子树是二叉查找树,并且右子树的最小值大于节点的值,该树即是二叉查找树;

(4)左子树不空,有子树为空,与(3)类似;

(5)左右子树都不空,则只有当左右子树都是二叉查找树,并且左子树的最大值小于该节点的值,右子树的最小值大于节点的值。

class Solution {
public:
    bool isValid(TreeNode *root, int &big, int &small)
    {
        if(root == NULL) {
            return true;
        }
        
        if(root->left == NULL && root->right == NULL) {
            big = root->val;
            small = root->val;
            return true;
        }
        else if(root->left == NULL && root->right) {
            int b = 0, s = 0;
            if(isValid(root->right, b, s) && s > root->val) {
                big = b;
                small = root->val;
                return true;
            }
            else
                return false;
        }
        else if(root->left && root->right == NULL) {
            int b = 0, s = 0;
            if(isValid(root->left, b, s) && b < root->val) {
                big = root->val;
                small = s;
                return true;
            }
            else
                return false;
        }
        
        bool ret = false;
        int b_left = 0, b_right = 0, s_left = 0, s_right = 0;

        if(isValid(root->left, b_left, s_left) && isValid(root->right, b_right, s_right) &&
            b_left < root->val && s_right > root->val)
            ret = true;
            
        big = b_right;
        small = s_left;
        
        return ret;
    }

    bool isValidBST(TreeNode *root) {
        // IMPORTANT: Please reset any member data you declared, as
        // the same Solution instance will be reused for each test case.
        int b = 0, s = 0;
        
        return isValid(root, b, s);
    }
};