题目链接

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.

分析:根据BST中序遍历序列有序的性质判断,只要在中序遍历二叉树的代码基础上加上判断相邻元素关系的语句即可                                                                                                                                      本文地址

 1 /**
 2  * Definition for binary tree
 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         // IMPORTANT: Please reset any member data you declared, as
14         // the same Solution instance will be reused for each test case.
15         //注意题目要求是 less than和greater than;
16         stack<TreeNode*> S;
17         TreeNode *pre = NULL, *p = root;
18         while(p || S.empty() == false)
19         {
20             while(p)
21             {
22                 S.push(p);
23                 p = p->left;
24             }
25             if(S.empty() == false)
26             {
27                 p = S.top();
28                 S.pop();
29                 if(pre && p->val <= pre->val)return false;
30                 pre = p;
31                 p = p->right;
32             }
33         }
34         return true;
35     }
36 };

【版权声明】转载请注明出处:http://www.cnblogs.com/TenosDoIt/p/3448413.html

转载于:https://www.cnblogs.com/TenosDoIt/p/3448413.html

LeetCode:Validate Binary Search Tree相关推荐

  1. LeetCode Validate Binary Search Tree(dfs)

    问题:判断树是否是合法的二叉搜索树 思路: 1.在dfs时,判断当前结点是否大于左子树的最大值,小于右子树的最小值.这是一种思路 2.另外一个思路是在中序遍历时,用个变量记录当前结点的前驱结点,再判断 ...

  2. Leetcode: Validate Binary Search Tree

    思路: 1. 难点在于构造递归函数的参数 2. 参数要包含上下界, 才能具有全局性. 第一次提交 WA 了, 因为写成来的判断函数是局部性的 代码: const int POS = 1E9; cons ...

  3. [CareerCup] 4.5 Validate Binary Search Tree 验证二叉搜索树

    4.5 Implement a function to check if a binary tree is a binary search tree. LeetCode上的原题,请参见我之前的博客Va ...

  4. LeetCode: 173. Binary Search Tree Iterator

    LeetCode: 173. Binary Search Tree Iterator 题目描述 Implement an iterator over a binary search tree (BST ...

  5. LeetCode 之 JavaScript 解答第98题 —— 验证二叉搜索树(Validate Binary Search Tree)

    Time:2019/4/24 Title: Vaildata Binary Search Tree Difficulty: Medium Author: 小鹿 题目:Vaildata Binary S ...

  6. LeetCode Closest Binary Search Tree Value II

    原题链接在这里:https://leetcode.com/problems/closest-binary-search-tree-value-ii/ 题目: Given a non-empty bin ...

  7. [leetcode] Recover Binary Search Tree

    Recover Binary Search Tree Two elements of a binary search tree (BST) are swapped by mistake. Recove ...

  8. LeetCode --- Validate Binary Search Tree

    题目链接 判断一颗二叉树是否是二叉搜索树(二叉排序树),也就是BST 如果该二叉树是BST, 那么对其中序遍历,所得序列一定是单调递增的(不考虑有重复数值的情况) 附上代码: 1 /** 2 * De ...

  9. 098 Validate Binary Search Tree 验证二叉搜索树

    给定一个二叉树,判断其是否是一个有效的二叉搜索树. 一个二叉搜索树有如下定义:     左子树只包含小于当前节点的数.     右子树只包含大于当前节点的数.     所有子树自身必须也是二叉搜索树. ...

最新文章

  1. 宁波大学计算机专业复试,2016年宁波大学信息科学与工程学院计算机专业考研复试题库. (1)...
  2. centos 默认mysql_centos改变mysql默认目录
  3. Login failed with an access denied error.
  4. RTX5 | 内存池01 - 内存池的使用
  5. Codeforces 55D Beautiful numbers (数位DP)
  6. sql not exists用法_牛客网数据库SQL实战详细剖析(5160)(更新完结)
  7. 线段树区间合并--询问某段区间内最长连续上升子序列即最长上升子串
  8. 单体 soa 微服务 区别_程序员必须要知道的SOA和微服务的区别
  9. Haar特征描述算子与人脸检测
  10. 网络规划设计师水平考试备考资料(11.分析总结)
  11. 电子计算机 隐形眼镜,戴隐形眼镜看电脑要注意什么
  12. 信息安全技术 代码安全审计规范
  13. Jupyter Notebook又一懒人神器,拖拽生成Python代码!
  14. 复习删除数组中的重复元素
  15. ansys matlab 温度场,ANSYS温度场分析步骤.pdf
  16. JavaIO系统解析
  17. Java中csv文件读写分析
  18. 计算机电子极域控制,极域电子教室控制软件操作手册
  19. python定界符有哪些_Python字符串
  20. 小白用Python抓取豆瓣高评分喜剧电影

热门文章

  1. SQL Server2005 日期字段与字符串比较的怪异问题
  2. McAfee Agent漏洞可导致黑客以Windows 系统权限运行代码
  3. 成都睿铂M6Pros近景摄影测量:西安古城墙数字化建设应用介绍
  4. 谈谈数据传输中的安全性
  5. 如何配置数据库ODBC数据源
  6. ppt转pdf软件免费版
  7. JSONTools Validator的使用
  8. 怎么将一个类的成员函数作为指针传递给另一个类的成员函数
  9. Python:使用ctypes库调用外部DLL
  10. Spring MVC 使用优化建议