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.

confused what "{1,#,2,3}" means?

OJ's Binary Tree Serialization:

The serialization of a binary tree follows a level order traversal, where '#' signifies a path terminator where no node exists below.

Here's an example:

   1
/ \
2 3
/
4
\
5

The above binary tree is serialized as "{1,2,3,#,#,4,#,#,5}".

 
题意:给定一颗二叉树,判断该树是否是二叉排序树。
二叉排序树的规定:
(1)如果存在左子树,则左子树的所有节点的值都要小于根节点的值。
(2)如果存在右子树,则右子树的所有节点的值都要大于根节点的值。
(3)如果一棵以root为根节点的树是二叉排序树,那么其左子树和右子树也必须是二叉排序树。
 
先给出我想到的解:
在之前有写到过用队列创建一颗二叉排序树,其过程类似于二叉树的中序遍历。
因此,可以反过来看出二叉排序树的中序遍历的结果组成的序列应该是有序的。
根据这种想法,可以得到如下代码:
class Solution {
public:
bool isValidBST(TreeNode *root) {
if(!root ||(!root->left && !root->right)) return true;
vector<int> vi;
vi.clear();
// 用非递归的方式对树进行中序遍历,将结果存放到vi数组中
stack<TreeNode* > s;
TreeNode *tmp=root;
while(!s.empty() || tmp){
if(tmp){
s.push(tmp);
tmp = tmp->left;
}else{
tmp = s.top();
s.pop();
vi.push_back(tmp->val);
tmp = tmp->right;
}
}
//对中序遍历的结果进行判断,注意不能有重复的数字
for(int i=;i<vi.size();i++){
if(vi[i]<=vi[i-]) return false;
}
return true;
}
};

转载请注明出处: http://www.cnblogs.com/double-win/ 谢谢!

[LeetCode 题解]: Validate Binary Search Tree的更多相关文章

  1. 【leetcode】Validate Binary Search Tree

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

  2. leetcode dfs Validate Binary Search Tree

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

  3. 【题解】【BST】【Leetcode】Validate Binary Search Tree

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

  4. Java for LeetCode 098 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. 【leetcode】Validate Binary Search Tree(middle)

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

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

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

随机推荐

  1. SpringData

    1.什么是SpringData?Spring Data 项目的目的是为了简化构建基于 Spring 框架应用的数据访问计数,包括非关系数据库.Map-Reduce 框架.云数据服务等等:另外也包含对关 ...

  2. Shiro的Subject和Sessoin的创建

    之前要先了解Session的来源Shiro session和Spring session一样吗? 创建Subject的位置 AbstractShiroFilter . doFilterInternal ...

  3. 用Eclipse进行远程Debug代码

    在新的公司,由于项目很大,在本机运行会很慢,所以都是在本地开发,在远程虚拟机上运行.这样就让我痛苦了,我怎么在本地Eclipse上进行debug调试呢,但是在公司前辈的指导下让我知道了本地Eclips ...

  4. ie8、9 post 跨域

    //显示浮层postAjax:function(url,param,callback){ var loadScore = layer_.load(1,{shade: [0.8,'#393D49']}) ...

  5. openLDAP 2

    一.安装OPENLDAP 二.打开安装目录中的文件 slapd.conf 三.安装完成后退出 编辑文本,输入以下内容,并命名为test.ldif dn: dc=company objectClass: ...

  6. Think In Java 读后感

         近期拜读了Think in Java 一书,这里是一些读后感.        此书不仅仅是市面上那种教会你怎么用系统API来编程的书,那种书太多.        此书不仅仅从头开始讲述了如何 ...

  7. java 多线程下载文件 以及URLConnection和HttpURLConnection的区别

    使用 HttpURLConnection 实现多线程下载文件 注意GET大写//http public class MultiThreadDownload { public static void m ...

  8. Opencv读取图片像素值

    #include <iostream>#include <opencv2/opencv.hpp> using namespace std;using namespace cv; ...

  9. table 合并行和列

    table合并行列,以及拆分 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "htt ...

  10. Python 中 "is" 与 "==" 操作有什么区别?

    转自:https://foofish.net/what-is-difference-between-is-and-euqals.html 在 Python 中,比较两个对象(变量)是否相等,可以用 & ...