98. Validate Binary Search Tree
来源:互联网 发布:mac foxmail 邮件备份 编辑:程序博客网 时间:2024/06/03 19:40
/*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.Example 1: 2 / \ 1 3Binary tree [2,1,3], return true.Example 2: 1 / \ 2 3Binary tree [1,2,3], return false.思路:BST的中序遍历的升序列*//** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */class Solution {public: bool isValidBST(TreeNode* root) { if(!root) return true; vector<int> v; inorder(root,v); for(int i=0;i<v.size()-1;i++) if(v[i]>=v[i+1]) return false; return true; } void inorder(TreeNode*root,vector<int>& v) { if(!root) return; inorder(root->left,v); v.push_back(root->val); inorder(root->right,v); } bool isValidBST(TreeNode* root) { long preVal=LONG_MIN; return inorder_chech(root,preVal); } bool inorder_chech(TreeNode* root,long &preVal) { if(!root) return true; if(!inorder_chech(root->left,preVal)) return false; if(root->val <= preVal) return false; preVal=root->val; if(!inorder_chech(root->right,preVal)) return false; return true; }};
阅读全文
0 0
- 98. Validate Binary Search Tree
- 98. Validate Binary Search Tree
- 98. Validate Binary Search Tree
- 98. Validate Binary Search Tree
- 98. Validate Binary Search Tree
- 98. Validate Binary Search Tree
- 98. Validate Binary Search Tree
- 98. Validate Binary Search Tree
- 98. Validate Binary Search Tree
- 98. Validate Binary Search Tree
- 98. Validate Binary Search Tree
- 98. Validate Binary Search Tree
- 98. Validate Binary Search Tree
- 98. Validate Binary Search Tree
- 98. Validate Binary Search Tree
- 98. Validate Binary Search Tree
- 98. Validate Binary Search Tree
- 98. Validate Binary Search Tree
- 快捷键大全-私人收藏
- [leetcode]664. Strange Printer
- 常见排序算法(时,空)复杂度总结及稳定性分析
- CentOS7.3安装部署wordpress
- SGISTL源码探究-vector容器(上)
- 98. Validate Binary Search Tree
- [单调队列] POJ2823
- ICPC2017网络赛(乌鲁木齐)E: Half-consecutive Numbers (大数)
- vyos
- Java NIO 介绍
- NOIP2016组合数问题(洛谷2822)
- html2canvas如何截高清全图
- Android中AsyncTask面试相关知识
- oracle 11g r2 Oracle数据库在防火墙中开放1521端口的方法