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. HTML5 Canvas 小例子 旋转的时钟

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  2. linux MYSQL大小写问题处理

    1)window下默认大小写不敏感,所以在window下.创建表 test后再想创建表TEST会报错.而linux下默认可以.认为是不同的两张表 2)linux创建数据库,安装完毕后 首要任务是在li ...

  3. javascript:查看一个图片是否加载完成

    查看一个图片是否加载完成:<img id="img1" src="http://pic1.xxx.com/wall/f/51c3bb99a21ea.jpg" ...

  4. ubuntu 该软件包现在的状态极为不妥 error

    rm -rf /var/lib/dpkg/info/yourerrorsofware* dpkg --remove --force-remove-reinstreq yourerrorsoftware ...

  5. 使用docker-compose创建启动容器时候提示服务名已存在

    今天在测试环境启动一个已有服务时候提示服务名已存在,造成这个原因是,前些天集群的一个节点挂了,导致这个节点上的docker容器全部没了,为了当时能正常使用就使用 docker-compose -H 1 ...

  6. shell编程小技巧(命令篇)

    本文主要介绍shell编程中一些好用的命令或者一些常见命令但比较少用却又好用的参数,目的是希望可以提高编码效率. df命令 常用命令 df / df -k / df -m / df -H / df - ...

  7. ARP协议,以及ARP欺骗

    1.定义: 地址解析协议,即ARP(Address Resolution Protocol),是根据IP地址获取物理地址的一个TCP/IP协议.主机发送信息时将包含目标IP地址的ARP请求广播到网络上 ...

  8. [cocos2d-x]移动平台游戏开发(图)

    FreeMind的.mm文件下载: http://yunpan.cn/cfL3QrrQVkVTd (提取码:a125)

  9. [PC]PHPCMS v9.5.6整合UEditer1.4.2

    ----------------------------------------------------------------------------------------------- 首先去U ...

  10. JAVA 16进制转ASCII -- 2018年5月25日 周五

    /** * 16进制转ASCII * * @param hex * @return */ public static String hex2Str(String hex) { StringBuilde ...