leetcode98
- class Solution {
- public:
- vector<int> V;
- void postTree(TreeNode* node) {
- if (node != NULL) {
- if (node->left != NULL) {
- postTree(node->left);
- }
- V.push_back(node->val);
- if (node->right != NULL) {
- postTree(node->right);
- }
- }
- }
- bool isValidBST(TreeNode* root) {
- postTree(root);
- for (int i = ; i < V.size(); i++) {
- if (V[i] <= V[i - ]) {
- return false;
- }
- }
- return true;
- }
- };
补充一个python的实现:
- class Solution:
- def __init__(self):
- self.lists = []
- def inOrder(self,root):
- if root != None:
- if root.left != None:
- self.inOrder(root.left)
- self.lists.append(root.val)
- if root.right != None:
- self.inOrder(root.right)
- def isValidBST(self, root: 'TreeNode') -> 'bool':
- self.inOrder(root)
- n = len(self.lists)
- for i in range(n-):
- if self.lists[i] >= self.lists[i+]:
- return False
- return True
leetcode98的更多相关文章
- leetcode98 Validate Binary Search Tree
题目: Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is define ...
- (BST 递归) leetcode98. Validate Binary Search Tree
Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defined as ...
- [Swift]LeetCode98. 验证二叉搜索树 | Validate Binary Search Tree
Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defined as ...
- LeetCode98. 验证二叉搜索树
验证二叉搜索树 * * https://leetcode-cn.com/problems/validate-binary-search-tree/description/ * * algor ...
- Leetcode98. Validate Binary Search Tree验证二叉搜索树
给定一个二叉树,判断其是否是一个有效的二叉搜索树. 假设一个二叉搜索树具有如下特征: 节点的左子树只包含小于当前节点的数. 节点的右子树只包含大于当前节点的数. 所有左子树和右子树自身必须也是二叉搜索 ...
- [LeetCode98]98. Validate Binary Search Tree判断二叉搜索树
判断二叉搜索树的方法是: 中序遍历形成递增序列 //全局变量记录中序遍历产生的序列,因为要递归,所以要用全局变量 List<Integer> list = new ArrayList< ...
- leetcode1305 All Elements in Two Binary Search Trees
""" Given two binary search trees root1 and root2. Return a list containing all the i ...
- leetcode_二叉树篇_python
主要是深度遍历和层序遍历的递归和迭代写法. 另外注意:因为求深度可以从上到下去查 所以需要前序遍历(中左右),而高度只能从下到上去查,所以只能后序遍历(左右中). 所有题目首先考虑root否是空.有的 ...
- LeetCode通关:连刷三十九道二叉树,刷疯了!
分门别类刷算法,坚持,进步! 刷题路线参考:https://github.com/youngyangyang04/leetcode-master 大家好,我是拿输出博客来督促自己刷题的老三,这一节我们 ...
随机推荐
- 【转】FMX 动态创建及销毁(释放free)对象
http://www.2pascal.com/thread-3037-1-1.html这是原文地址. (* ********************************************** ...
- 2019-04-15-day032-多进程介绍
内容回顾 基于原生socket的udp协议实现将client端发送过来的消息放到字典中 字典的key是所有客户端的地址,value是一个列表 io :输入输出, 输入到内存,向内存输入 从内存中向外( ...
- c#抓屏功能在DPI缩放后,截到的图片不完整的问题
/// <summary> /// 获取屏幕快照 /// </summary> /// <returns></returns> public stati ...
- CentOS版Linux系统上运行ASP.NET应用
一.安装: 1. 安装Apache Http Server yum install httpd2. 安装Mono yum install mono3. 安装Mono插件,用来处理ASP.NET请求 y ...
- CentOS 7 命令行安装TeamViewer
由于要通过要远程登录到内网的电脑(一台笔记本),用于在紧急情况下处理服务器故障.刚开始准备使用ssh端口转发,无奈vps转发速度太慢. 后面考虑使用TeamViewer远程控制Windows桌面,但是 ...
- webpack的入门教程
webpack是模块打包工具/前端资源加载.它是根据模块的依赖关系进行静态分析,然后将这些模块按照指定的规则生成对应的静态资源. webpack可以将css.less.js转换为一个静态文件,减少了页 ...
- C++之string类型详解
C++之string类型详解 之所以抛弃char*的字符串而选用C++标准程序库中的string类,是因为他和前者比较起来,不必担心内存是否足够.字符串长度等等,而且作为一个泛型类出现,他集成的操作函 ...
- solr增加中文分析器
我的solr版本是5.3.0 1将jar包ik-analyzer-solr5-5.x.jar放入sor的web-inf的lib里面 2 在web-inf下面新建classes目录,再新增三个配置文件: ...
- js 关于定时器的知识点。
Js的同步和异步 同步:代码从上到下执行. 异步:每个模块执行自己的,同时执行. js本身就是同步的,但是需要记住四个地方是异步. Js的异步 1.定时器 2.ajax 3事件的绑定 4. ...
- JDK下载与安装、 Eclipse下载与使用、 Tomcat下载与使用、 MySQL安装与使用
前言 本文将介绍JDK的下载与安装,eclipse的下载与使用,Tomcat的下载与使用,MySQL的安装与使用. JDK下载与安装 一.JRE与JDK介绍 java是当前比较流行的一种编程语言,当我 ...