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. 1
  2. / \
  3. 2 3
  4. /
  5. 4
  6. \
  7. 5

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

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

转载请注明出处: 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. MySQL多实例介绍

    我们前面已经做了MySQL数据库的介绍以及为什么选择MySQL数据库,最后介绍了MySQL数据库在Linux系统下的多种安装方式,以及讲解了MySQL的二进制方式单实例安装.基础优化等,下面给大家讲解 ...

  2. 温故而知新-WebSocket 教程

    一.为什么需要 WebSocket? 初次接触 WebSocket 的人,都会问同样的问题:我们已经有了 HTTP 协议,为什么还需要另一个协议?它能带来什么好处? 答案很简单,因为 HTTP 协议有 ...

  3. oringin 画图

    oringin做图输出矢量图方法: 右击图区,选择copy page 在Word文档中直接粘贴即可 oringin做图调整图边距: tool->option->page->margi ...

  4. 七牛云存储的 Javascript Web 前端文件上传

    因为我的个人网站 restran.net 已经启用,博客园的内容已经不再更新.请访问我的个人网站获取这篇文章的最新内容,七牛云存储的 Web 前端文件上传 七牛是不错的云存储产品,特别是有免费的配额可 ...

  5. 关于在Arduino中调用DS1302模块

    DS1302时钟模块中的电池是起掉电保存作用的,在实际运行中必须给他的GND和VCC供电,否则得到的是错误的时间. 也就是说,电池是保存日期的,而无法提供芯片正常运行所需的电力. 从芯片引脚上可以看出 ...

  6. 将网页的部分位置嵌入Html网页

    <div align="center" style="margin:0 auto;"> <div style="width:500p ...

  7. Custom Draw 基础(转载)

    common control 4.7版本介绍了一个新的特性叫做Custom Draw,这个名字显得模糊不清,让人有点摸不着头脑,而且MSDN里也只给出了一些如风的解释和例子,没有谁告诉你你想知道的,和 ...

  8. 使用crontab设置定时任务

    配置文件 crontab主要的配置文件如下: /etc/crontab:系统cron表 /etc/cron.d/*:保存由软件包安装脚本创建的cron文件的目录 /var/spool/cron/*:保 ...

  9. 201671010127 2016-2017-18 Java期末总结

    通过本学期Java课程的学习,我对于面向对象的编程语言有了进一步的了解.首先面向对象编程的特点是抽象.封装.继承.多态.由于已经学过c语言,所以对Java的学习实际上是从第四章对向与类开始的,然后学习 ...

  10. UV mapping

    [UV mapping] UV mapping is the 3D modeling process of making a 2D image representation of a 3D model ...