题目:

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.

代码:

  1. /**
  2. * Definition for a binary tree node.
  3. * struct TreeNode {
  4. * int val;
  5. * TreeNode *left;
  6. * TreeNode *right;
  7. * TreeNode(int x) : val(x), left(NULL), right(NULL) {}
  8. * };
  9. */
  10. class Solution {
  11. public:
  12. bool isValidBST(TreeNode* root) {
  13. if (!root) return true;
  14. stack<TreeNode *> sta;
  15. vector<int> traversal;
  16. TreeNode *p = root;
  17. while ( !sta.empty() || p )
  18. {
  19. if (p){
  20. sta.push(p);
  21. p = p->left;
  22. }
  23. else{
  24. p = sta.top();
  25. sta.pop();
  26. if ( !traversal.empty() && traversal[traversal.size()-]>=p->val ) return false;
  27. traversal.push_back(p->val);
  28. p = p->right;
  29. }
  30. }
  31. return true;
  32. }
  33. };

tips:

中序遍历BST;并保留每次访问的元素;如果中序遍历的前一个元素不小于后一个元素,则返回false;全部遍历过后无问题,则返回true。

题目中有一个test case是:[-2147483648],即INT_MIN。由于有这个非常极端的case存在,之前那种设一个pre = INT_MIN的办法就行不通了,暂时老老实实用一个vector来代替。

==========================================

一开始刷这道题总想用递归,后来看的之前的代码,BST这种判断有效的,用中序遍历可能更好一些。

  1. /**
  2. * Definition for a binary tree node.
  3. * struct TreeNode {
  4. * int val;
  5. * TreeNode *left;
  6. * TreeNode *right;
  7. * TreeNode(int x) : val(x), left(NULL), right(NULL) {}
  8. * };
  9. */
  10. class Solution {
  11. public:
  12. bool isValidBST(TreeNode* root) {
  13. stack<TreeNode*> sta;
  14. TreeNode* curr = root;
  15. vector<int> vals;
  16. while ( !sta.empty() || curr )
  17. {
  18. if ( curr )
  19. {
  20. sta.push(curr);
  21. curr = curr->left;
  22. }
  23. else
  24. {
  25. curr = sta.top();
  26. vals.push_back(curr->val);
  27. sta.pop();
  28. if ( vals.size()> && vals[vals.size()-]<=vals[vals.size()-] ) return false;
  29. curr = curr->right;
  30. }
  31. }
  32. return true;
  33. }
  34. };

【Validate Binary Search Tree】cpp的更多相关文章

  1. 【二叉查找树】03验证是否为二叉查找树【Validate Binary Search Tree】

    本质上是递归遍历左右后在与根节点做判断,本质上是后序遍历 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ ...

  2. 【Recover Binary Search Tree】cpp

    题目: Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree without chan ...

  3. 【Lowest Common Ancestor of a Binary Search Tree】cpp

    题目: Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in th ...

  4. 【遍历二叉树】07恢复二叉搜索树【Recover Binary Search Tree】

    开一个指针数组,中序遍历这个二叉搜索树,将节点的指针依次保存在数组里, 然后寻找两处逆序的位置, 中序便利里BST得到的是升序序列 ++++++++++++++++++++++++++++++++++ ...

  5. 【Unique Binary Search Trees】cpp

    题目: Given n, how many structurally unique BST's (binary search trees) that store values 1...n? For e ...

  6. 【Convert Sorted List to Binary Search Tree】cpp

    题目: Given a singly linked list where elements are sorted in ascending order, convert it to a height ...

  7. 【Convert Sorted Array to Binary Search Tree】cpp

    题目: Given an array where elements are sorted in ascending order, convert it to a height balanced BST ...

  8. 【leetcode】Validate Binary Search Tree

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

  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. .NET中 使用数组的注意事项

    1.初始值问题 对于int.double.float等一些值类型数组,没有赋值的情况下, 默认值是0: 而对于String 等引用类型,初始值为null. 2.IndexOutOfRangeExcep ...

  2. iConvert Icons 图标转换生成利器,支持Windows, Mac OS X, Linux, iOS,和Android等系统

    这是一款在线图标转换工具,生成的图标支持Windows, Mac OS X, Linux, iOS, 和 Android等主流系统. 可以上传图标文件转化成另一个平台下的图标文件,例如将windows ...

  3. JQuery Validate使用总结

    本文参考了  http://www.cnblogs.com/linjiqin/p/3431835.html 可以在mvc 或webform项目中使用,可以方便快捷的对前端表单进行校验 一.导入两个js ...

  4. C# Datetime类常用技巧

    C#类常用技巧 //今天DateTime.Now.Date.ToShortDateString();//昨天,也就是今天的日期减一DateTime.Now.AddDays(-1).ToShortDat ...

  5. 打包程序tar

    tar  [选项] tar文件 [目录文件] 常用选项如下所述: -c:创建新的归档文件 -d:检查归档文件与指定目录的差异 -r:向归档文件中追加文件 -v:显示命令的执行日期 -u:只有当需要追加 ...

  6. Maven的HTTP代理设置

    http://blog.sina.com.cn/s/blog_4f925fc30102ed3y.html   第一.检测本地网络是否不能直接访问Maven的远程仓库,命令为ping repo1.mav ...

  7. Linux下的Source命令及脚本的执行方式解析

    Linux Source命令及脚本的执行方式解析 http://blog.csdn.net/wangyangkobe/article/details/6595143 当我修改了/etc/profile ...

  8. iostat命令简单说说

    tps: 每秒钟发送到的I/O请求数. Blk_read /s: 每秒读取的block数 Blk_wrtn/s: 每秒写入的block数 Blk_read: 读入的block总数 Blk_wrtn: ...

  9. centos+php+coreseek+sphinx+mysql之二sphinx配置篇

    先进入文件夹进行以下操作 cd /usr/local/coreseek/etc cp sphinx.conf.dist sphinx.conf source src1 { sql_host = 127 ...

  10. 在 Windows 7 環境安裝 Python 2.6.6

    目前 Python 的最穩定的版本是 2.7.3 及 3.2.3,因為 2.x 與 3.x 語法並不是完全相容,在各版本之間也有些差異,所以建議還是各自安裝需要的版本… 艾小克工作環境是使用 2.6 ...