一天一道LeetCode

本系列文章已全部上传至我的github,地址:ZeeCoder‘s Github

欢迎大家关注我的新浪微博,我的新浪微博

欢迎转载,转载请注明出处

(一)题目

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.

Example 1:

  2

  / \

 1 3

Binary tree [2,1,3], return true.

Example 2:

  1

  / \

 2 3

Binary tree [1,2,3], return false.

(二)解题

题目大意:给定一个二叉树,判断该树是不是二叉搜索树。

解题思路:二叉搜索树满足,对于一个父节点,左子树所有节点的数值小于父节点的数值,右子树的所有节点的数值大于父节点的数值。

我的做法是采用递归的方法,并用一对min和max来记录左/右子树需要满足的数值范围。对于一个父节点root,需要满足在(min,max)范围内,然后判断左右节点,需要满足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) {
        if(root==NULL) return true;
        return dfsValidBST(root,-2147483649,2147483648);//这里初始范围设为INT_MIN-1,INT_MAX+1
    }
    bool dfsValidBST(TreeNode* root , long min , long max)
    {
        if(root->val<=min||root->val>=max) return false;
        bool left = true;
        bool right = true;
        if(root->left!=NULL)//left不为空
        {
            if(root->left->val < root->val) left = dfsValidBST(root->left,min,root->val);//满足条件继续往左子树搜索,min和max更新
            else left = false;
        }
        if(root->right!=NULL)//right不为空
        {
            if(root->right->val > root->val) right = dfsValidBST(root->right,root->val,max);//满足条件继续往右子树搜索,min和max更新
            else right = false;
        }
        return left&&right;//二者都为true,才返回true
    }
};

【一天一道LeetCode】#98. Validate Binary Search Tree的更多相关文章

  1. [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 ...

  2. 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 ...

  3. 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 ...

  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(是否是二叉搜索树) ☆☆☆

    描述 解析 二叉搜索树,其实就是节点n的左孩子所在的树,每个节点都小于节点n. 节点n的右孩子所在的树,每个节点都大于节点n. 定义子树的最大最小值 比如:左孩子要小于父节点:左孩子n的右孩子要大于n ...

  6. Leetcode 笔记 98 - Validate Binary Search Tree

    题目链接:Validate Binary Search Tree | LeetCode OJ Given a binary tree, determine if it is a valid binar ...

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

  8. 【leetcode】Validate Binary Search Tree

    Validate Binary Search Tree Given a binary tree, determine if it is a valid binary search tree (BST) ...

  9. leetcode dfs Validate Binary Search Tree

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

随机推荐

  1. jquery easyui datagrid 排序列

    点击排序列,将获取参数有:page=1&rows=10&sort=UserName&order=desc c#后台获取sort跟order参数 string sortColum ...

  2. 去掉Bootstrap fileinput缩略图上面的上传删除按钮?

  3. 关于一些基础的Java问题的解答(六)

    26. ThreadPool用法与优势 ThreadPool即线程池,它是JDK1.5引入的Concurrent包中用于处理并发编程的工具.使用线程池有如下好处: 降低资源消耗:通过重复利用已创建的线 ...

  4. dubbo服务的发布和调用

    Dubbo是分布式服务架构,是一个优秀的开源服务型框架,使得应用可以通过高性能的rpc实现服务的输入和输出功能.其实dubbo就是资源调度和治理中心的管理工具. 发布dubbo服务:在提供服务的应用中 ...

  5. Java 读取Excel文件

    https://www.cnblogs.com/wwzyy/p/5962076.html   先把上面的参考博客看了,如果会导入包的话,下面的教程就直接忽略emm     这时候,你应该把jar包下载 ...

  6. Oracle中rownum的说明及使用技巧

    一.rownum的说明 rownum是oracle特有的一个关键字. (1)对于基表,在insert记录时,oracle就按照insert的顺序,将rownum分配给每一行记录,因此在select一个 ...

  7. Android Studio精彩案例(五)《JSMS短信验证码功能实现》

    转载本专栏文章,请注明出处,尊重原创 .文章博客地址:道龙的博客 很多应用刚打开的时候,让我们输入手机号,通过短信验证码来登录该应用.那么,这个场景是怎么实现的呢?其实是很多开放平台提供了短信验证功能 ...

  8. UE4使用UMG接口操作界面

    原文链接:http://gad.qq.com/article/detail/7181131 本文首发腾讯GAD开发者平台,未经允许,不得转载 UE4的蓝图之强大让人欲罢不能,但是实际在项目的开发中,C ...

  9. 豌豆夹Redis解决方案Codis源码剖析:Dashboard

    豌豆夹Redis解决方案Codis源码剖析:Dashboard 1.不只是Dashboard 虽然名字叫Dashboard,但它在Codis中的作用却不可小觑.它不仅仅是Dashboard管理页面,更 ...

  10. 【Unity Shader】新书封面 — Low Polygon风格的渲染

    写在前面 最近又开心又担心,因为我的书马上就要上市了,开心当然是因为等了这么久终于可以如愿了,担心是因为不少人对它的期待都很大,我第一次写书,能力也有限,不知道能不能让大家满意,让大家也都喜欢上它.不 ...