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 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相关推荐
- LeetCode Validate Binary Search Tree(dfs)
问题:判断树是否是合法的二叉搜索树 思路: 1.在dfs时,判断当前结点是否大于左子树的最大值,小于右子树的最小值.这是一种思路 2.另外一个思路是在中序遍历时,用个变量记录当前结点的前驱结点,再判断 ...
- Leetcode: Validate Binary Search Tree
思路: 1. 难点在于构造递归函数的参数 2. 参数要包含上下界, 才能具有全局性. 第一次提交 WA 了, 因为写成来的判断函数是局部性的 代码: const int POS = 1E9; cons ...
- [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 ...
- LeetCode: 173. Binary Search Tree Iterator
LeetCode: 173. Binary Search Tree Iterator 题目描述 Implement an iterator over a binary search tree (BST ...
- LeetCode 之 JavaScript 解答第98题 —— 验证二叉搜索树(Validate Binary Search Tree)
Time:2019/4/24 Title: Vaildata Binary Search Tree Difficulty: Medium Author: 小鹿 题目:Vaildata Binary S ...
- LeetCode Closest Binary Search Tree Value II
原题链接在这里:https://leetcode.com/problems/closest-binary-search-tree-value-ii/ 题目: Given a non-empty bin ...
- [leetcode] Recover Binary Search Tree
Recover Binary Search Tree Two elements of a binary search tree (BST) are swapped by mistake. Recove ...
- LeetCode --- Validate Binary Search Tree
题目链接 判断一颗二叉树是否是二叉搜索树(二叉排序树),也就是BST 如果该二叉树是BST, 那么对其中序遍历,所得序列一定是单调递增的(不考虑有重复数值的情况) 附上代码: 1 /** 2 * De ...
- 098 Validate Binary Search Tree 验证二叉搜索树
给定一个二叉树,判断其是否是一个有效的二叉搜索树. 一个二叉搜索树有如下定义: 左子树只包含小于当前节点的数. 右子树只包含大于当前节点的数. 所有子树自身必须也是二叉搜索树. ...
最新文章
- 宁波大学计算机专业复试,2016年宁波大学信息科学与工程学院计算机专业考研复试题库. (1)...
- centos 默认mysql_centos改变mysql默认目录
- Login failed with an access denied error.
- RTX5 | 内存池01 - 内存池的使用
- Codeforces 55D Beautiful numbers (数位DP)
- sql not exists用法_牛客网数据库SQL实战详细剖析(5160)(更新完结)
- 线段树区间合并--询问某段区间内最长连续上升子序列即最长上升子串
- 单体 soa 微服务 区别_程序员必须要知道的SOA和微服务的区别
- Haar特征描述算子与人脸检测
- 网络规划设计师水平考试备考资料(11.分析总结)
- 电子计算机 隐形眼镜,戴隐形眼镜看电脑要注意什么
- 信息安全技术 代码安全审计规范
- Jupyter Notebook又一懒人神器,拖拽生成Python代码!
- 复习删除数组中的重复元素
- ansys matlab 温度场,ANSYS温度场分析步骤.pdf
- JavaIO系统解析
- Java中csv文件读写分析
- 计算机电子极域控制,极域电子教室控制软件操作手册
- python定界符有哪些_Python字符串
- 小白用Python抓取豆瓣高评分喜剧电影