[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.
confused what "{1,#,2,3}"
means?
The serialization of a binary tree follows a level order traversal, where '#' signifies a path terminator where no node exists below.
Here's an example:
- 1
- / \
- 2 3
- /
- 4
- \
- 5
The above binary tree is serialized as "{1,2,3,#,#,4,#,#,5}"
.
- class Solution {
- public:
- bool isValidBST(TreeNode *root) {
- if(!root ||(!root->left && !root->right)) return true;
- vector<int> vi;
- vi.clear();
- // 用非递归的方式对树进行中序遍历,将结果存放到vi数组中
- stack<TreeNode* > s;
- TreeNode *tmp=root;
- while(!s.empty() || tmp){
- if(tmp){
- s.push(tmp);
- tmp = tmp->left;
- }else{
- tmp = s.top();
- s.pop();
- vi.push_back(tmp->val);
- tmp = tmp->right;
- }
- }
- //对中序遍历的结果进行判断,注意不能有重复的数字
- for(int i=;i<vi.size();i++){
- if(vi[i]<=vi[i-]) return false;
- }
- return true;
- }
- };
转载请注明出处: http://www.cnblogs.com/double-win/ 谢谢!
[LeetCode 题解]: Validate Binary Search Tree的更多相关文章
- 【leetcode】Validate Binary Search Tree
Validate Binary Search Tree Given a binary tree, determine if it is a valid binary search tree (BST) ...
- leetcode dfs Validate Binary Search Tree
Validate Binary Search Tree Total Accepted: 23828 Total Submissions: 91943My Submissions Given a bin ...
- 【题解】【BST】【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 ...
- Java for LeetCode 098 Validate Binary Search Tree
Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defined as ...
- [LeetCode] 98. Validate Binary Search Tree 验证二叉搜索树
Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defined as ...
- Leetcode 98. Validate Binary Search Tree
Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defined as ...
- 【leetcode】Validate Binary Search Tree(middle)
Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defined as ...
- leetcode 98 Validate Binary Search Tree ----- java
Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defined as ...
- [leetcode]98. Validate Binary Search Tree验证二叉搜索树
Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defined as ...
随机推荐
- MySQL多实例介绍
我们前面已经做了MySQL数据库的介绍以及为什么选择MySQL数据库,最后介绍了MySQL数据库在Linux系统下的多种安装方式,以及讲解了MySQL的二进制方式单实例安装.基础优化等,下面给大家讲解 ...
- 温故而知新-WebSocket 教程
一.为什么需要 WebSocket? 初次接触 WebSocket 的人,都会问同样的问题:我们已经有了 HTTP 协议,为什么还需要另一个协议?它能带来什么好处? 答案很简单,因为 HTTP 协议有 ...
- oringin 画图
oringin做图输出矢量图方法: 右击图区,选择copy page 在Word文档中直接粘贴即可 oringin做图调整图边距: tool->option->page->margi ...
- 七牛云存储的 Javascript Web 前端文件上传
因为我的个人网站 restran.net 已经启用,博客园的内容已经不再更新.请访问我的个人网站获取这篇文章的最新内容,七牛云存储的 Web 前端文件上传 七牛是不错的云存储产品,特别是有免费的配额可 ...
- 关于在Arduino中调用DS1302模块
DS1302时钟模块中的电池是起掉电保存作用的,在实际运行中必须给他的GND和VCC供电,否则得到的是错误的时间. 也就是说,电池是保存日期的,而无法提供芯片正常运行所需的电力. 从芯片引脚上可以看出 ...
- 将网页的部分位置嵌入Html网页
<div align="center" style="margin:0 auto;"> <div style="width:500p ...
- Custom Draw 基础(转载)
common control 4.7版本介绍了一个新的特性叫做Custom Draw,这个名字显得模糊不清,让人有点摸不着头脑,而且MSDN里也只给出了一些如风的解释和例子,没有谁告诉你你想知道的,和 ...
- 使用crontab设置定时任务
配置文件 crontab主要的配置文件如下: /etc/crontab:系统cron表 /etc/cron.d/*:保存由软件包安装脚本创建的cron文件的目录 /var/spool/cron/*:保 ...
- 201671010127 2016-2017-18 Java期末总结
通过本学期Java课程的学习,我对于面向对象的编程语言有了进一步的了解.首先面向对象编程的特点是抽象.封装.继承.多态.由于已经学过c语言,所以对Java的学习实际上是从第四章对向与类开始的,然后学习 ...
- UV mapping
[UV mapping] UV mapping is the 3D modeling process of making a 2D image representation of a 3D model ...