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.

描述:即判断一棵树是不是二叉搜索树(左孩子小于父节点值,右孩子大于父亲节点值,且其左子树和右子树也同时满足BST)

思路:双层递归。

第一层,递归每一个节点是否满足,左边所有节点都小于它,右边所有节点都大于它。

第二层,如何来实现判断,左边所有节点都小于根,右边所有节点大于根。(每个节点都要作为根)。

特殊:root为空时,return true

代码:

class Solution {
public:
bool isLeftValid(TreeNode *root,int val){//某个根的左树,val该根的值
if(root==NULL)
return true;
return root->val<val&&isLeftValid(root->left,val)&&isLeftValid(root->right,val);
}
bool isRightValid(TreeNode *root,int val){//某个根的右树,val该根的值
if(root==NULL)
return true;
return root->val>val&&isRightValid(root->left,val)&&isRightValid(root->right,val);
}
bool isValidBST(TreeNode *root) {
if(root==NULL)
return true;
bool flag=isLeftValid(root->left,root->val)&&isRightValid(root->right,root->val);
if(!flag)
return false;
return isValidBST(root->left)&&isValidBST(root->right);
}
};

Validate Binary Search Tree(DFS)的更多相关文章

  1. leetcode dfs Validate Binary Search Tree

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

  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

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

  4. Validate Binary Search Tree

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

  5. 【leetcode】Validate Binary Search Tree

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

  6. LintCode Validate Binary Search Tree

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

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

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

  9. 【LeetCode练习题】Validate Binary Search Tree

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

随机推荐

  1. js对象数组深度去重和深度排序

    使用collect.js处理数组和对象 https://github.com/ecrmnn/collect.js/# 引入collect.js https://github.com/ecrmnn/co ...

  2. myeclipse 跟踪struts 源码失败

    解决办法: 找到工程jar包所在的位置,点击右键:properties 点击external folder 找到  这个包下的src文件夹 导入之后, 源码会变色

  3. HDU_1542_(树状数组)

    Stars Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submi ...

  4. CAD参数绘制多段线(com接口)

    多段线又被称为多义线,表示一起画的都是连在一起的一个复合对象,可以是直线也可以是圆弧并且它们还可以加不同的宽度. 主要用到函数说明: _DMxDrawX::PathLineTo 把路径下一个点移到指定 ...

  5. shell 循环 read line

    cat dockerlist |while read line;do docker rmi  $line ;done

  6. python3+beautifulSoup4.6抓取某网站小说(三)网页分析,BeautifulSoup解析

    本章学习内容:将网站上的小说都爬下来,存储到本地. 目标网站:www.cuiweijuxs.com 分析页面,发现一共4步:从主页进入分版打开分页列表.打开分页下所有链接.打开作品页面.打开单章内容. ...

  7. 梯度提升决策树(GBDT)与XGBoost、LightGBM

    今天是周末,之前给自己定了一个小目标:每周都要写一篇博客,不管是关于什么内容的都行,关键在于总结和思考,今天我选的主题是梯度提升树的一些方法,主要从这些方法的原理以及实现过程入手讲解这个问题. 本文按 ...

  8. 尝试用React写几个通用组件 - 带搜索功能的下拉列表,开关切换按钮,弹出框

    尝试用React写几个通用组件 - 带搜索功能的下拉列表,开关切换按钮,弹出框 近期正在逐步摸索学习React的用法,尝试着写几个通用型的组件,整体项目还是根据webpack+react+css-me ...

  9. 笔试算法题(33):烙饼排序问题 & N!阶乘十进制末尾0的个数二进制最低1的位置

    出题:不同大小烙饼的排序问题:对于N块大小不一的烙饼,上下累在一起,由于一只手托着所有的饼,所以仅有一只手可以翻转饼(假设手足够大可以翻转任意块数的 饼),规定所有的大饼都出现在小饼的下面则说明已经排 ...

  10. 大前端之HTML5\CSS3