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.

思路:由于Binary Tree的中序遍历结果是正序,所以可以检查中序遍历的结果是否递增

/**
* 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;
TreeNode* pre = NULL; //we don't need to save all nodes, only a previous node is enough to know whether it's an increase sequence
return inOrderTraverse(root,pre);
} bool inOrderTraverse(TreeNode* root, TreeNode* &pre){ //important to use &, otherwise new object will use a new address and the result won't bring back to caller
//visit left child
if(root->left)
if(!inOrderTraverse(root->left,pre))
return false; //visit root
if(pre==NULL) pre = new TreeNode(root->val);
else if(root->val > pre->val) pre->val = root->val;
else return false; //visit right child
if(root->right) return inOrderTraverse(root->right, pre);
else return true;
}
};

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

  1. Leetcode 笔记 98 - Validate Binary Search Tree

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

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

  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 验证二叉搜索树

    Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defined as ...

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

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

  8. LeetCode OJ 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 ...

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

随机推荐

  1. PHP + Apache 在 Linux(centos7)系统下的环境搭建,基于 yum

    (本文采用的是 Centos7 的操作系统,简单起见,以下全部采用 yum 安装,有这么好用的东西为什么要自己去一个一个编译呢) 1, 安装 Apache  => yum -y install ...

  2. [记录] Ubuntu 配置Apache虚拟站点

    版本 Ubuntu 16.04 1 . 首先找到Apapche配置文件夹  /etc/apache2/ apache2.conf conf-enabled magic mods-enabled sit ...

  3. MBR (主引导记录)

    概念 主引导记录(MBR,Main Boot Record)是位于磁盘最前边的一段引导(Loader)代码.它负责磁盘操作系统(DOS)对磁盘进行读写时分区合法性的判别.分区引导信息的定位,它由磁盘操 ...

  4. 试用bus hound来分析STM32CubeMX来生成USB_HID_Mouse工程

    Bus Hound (x64). Complements of www.perisoft.net STM32_HID_mouse Device - Device ID (followed by the ...

  5. springboot 多环境选择

    1.配置开发环境(开发环境) 2. application.yml  环境选择 3.cmd  切换环境 4. 设置环境调用方法 5. 另一方法

  6. Windows下MongoDB安装配置

    一.安装 官网下载,一般选择community server版本下载,如果是企业可以选择enterprise版本,个人使用的话community就可以了,附上链接:https://www.mongod ...

  7. linux shell 语法学习

    文件比较运算符-e filename 如果 filename存在,则为真 [ -e /var/log/syslog ]-d filename 如果 filename为目录,则为真 [ -d /tmp/ ...

  8. 前后台联调,突然所有的接口请求状态为200,但response什么都没有只有一句灰色的英文

    问题解决了,图就下次遇到截图补上: 解决问题的方法,是让后台查看数据库是否锁库,或者更改什么配置文件例如.xml文件,还有就是ip错误:

  9. Structs复习 命名空间

    引入jar包 web.xml <?xml version="1.0" encoding="UTF-8"?> <web-app version= ...

  10. C++ 使用VS2010创建MFC ActiveX工程项目

    1.ActiveX的基本概念 ActiveX控件可以看作是一个极小的服务器应用程序,它不能独立运行,必须嵌入到某个容器程序中,与该容器一起运行.这个容器包括WEB网页,应用程序窗体等... Activ ...