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. Android学习笔记(十三) Handler

    可用于解决上一则笔记所提到的WorkerThread无法修改UI控件的问题 一.Handler.Looper和MessageQueue的基本原理 Handler把消息对象放到MessageQueue当 ...

  2. Android学习笔记(十) Activity的生命周期

    一.如何在一个应用程序中定义多个Activity -定义一个类,继承Activity -复写onCreate() setContentView(R.layout.secondLayout):设定该Ac ...

  3. 12. binary search Trees

    12. binary search Trees    The search tree data structure supports many dynamic-set operations,inclu ...

  4. JPA调用存储过程

    @Transactional public BasAccount findByAccount(String account) { System.out.println(account); Query ...

  5. js 类似于移动端购物车删除,左移动删除

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  6. CreateWindowEx详解

    语法: HWND CreateWindowEx( DWORD dwExStyle, LPCTSTR lpClassName, LPCTSTR lpWindowName, DWORD dwStyle, ...

  7. CAD交互绘制直线(com接口)

    用户可以在控件视区任意位置绘制直线. 主要用到函数说明: _DMxDrawX::DrawLine 绘制一个直线.详细说明如下: 参数 说明 DOUBLE dX1 直线的开始点x坐标 DOUBLE dY ...

  8. vue+webpack静态资源路径引用

    处理静态资产 你可能已经注意到,在项目结构中我们有两个静态资产目录:src/assets和static/.他们之间有什么区别? 要回答这个问题,我们首先需要了解Webpack如何处理静态资产.在*.v ...

  9. uploadify插件可选参数的详细介绍

    Uploadify 是一个JQuery插件,它协助你轻松简单的将一个或多个文件上传至你的网站.  它需要Flash控件和后台开发语言的支持,丰富的参数配置,同时也简单易用,让你轻松上手.      官 ...

  10. 03CSS内容背景

    CSS内容背景 设置背景颜色——background-color  插入背景图片——background-image  设置背景图片位置——background-position 设置重复背景图片—— ...