lintcode-验证查找二叉树-95
来源:互联网 发布:sam软件和au 编辑:程序博客网 时间:2024/06/05 02:09
给定一个二叉树,判断它是否是合法的二叉查找树(BST)
一棵BST定义为:
- 节点的左子树中的值要严格小于该节点的值。
- 节点的右子树中的值要严格大于该节点的值。
- 左右子树也必须是二叉查找树。
样例
一个例子:
1 / \ 2 3 / 4 \ 5
上述这棵二叉树序列化为"{1,2,3,#,#,4,#,#,5}"
.
解题思路:如果是查找二叉树,中序遍历必然有序。
/** * Definition of TreeNode: * class TreeNode { * public: * int val; * TreeNode *left, *right; * TreeNode(int val) { * this->val = val; * this->left = this->right = NULL; * } * } */class Solution {public: void LDR(vector<int> &vec,TreeNode *root){ if(!root) return ; LDR(vec,root->left); vec.push_back(root->val); LDR(vec,root->right); } bool isValidBST(TreeNode *root) { if(!root) return true; vector<int> vec; LDR(vec,root); int last=vec[0]; vec.erase(vec.begin()); for(auto e:vec){ if(last>=e) return false; last=e; } return true; }};
0 0
- lintcode-验证查找二叉树-95
- lintcode(95)验证二叉查找树
- LintCode 95-验证二叉查找树
- LintCode:验证二叉查找树
- LintCode : 验证二叉查找树
- Lintcode 验证二叉查找树
- 验证二叉查找树-LintCode
- 验证二叉查找树-LintCode
- LintCode 97 验证二叉查找树
- LINTCODE —— 验证二叉查找树
- LintCode-分治-验证二叉查找树
- 验证二叉查找树—LintCode
- lintcode validate-binary-search-tree 验证二叉查找树
- **[Lintcode]Validate Binary Search Tree 验证二叉查找树
- lintcode 二叉查找树总结
- lintcode二叉查找树总结
- 验证满二叉树-LintCode
- [LintCode]95.验证二叉查找树(二叉排序树/二叉搜索树) 中序遍历
- extjs关于treePanel+chekBox全部选中以及清空选中问题探讨
- java 排序算法---插入排序
- The Factor(hdu5428)
- Android的AsyncTask和数据存储
- 重新认识二级指针(Pointers to Pointers)
- lintcode-验证查找二叉树-95
- 菜鸟系列——双连通分量
- list.h 解析
- Urimoo做试卷
- 什么是J2EE
- 重启oracle数据库
- 用busybox搭建最简单的Linux文件系统
- Android关于Theme.AppCompat相关问题的深入分析
- Service IntentService Android中的线程 MediaPlayer