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 3

Binary tree [2,1,3], return true.

Example 2:

    1
/ \
2 3

Binary tree [1,2,3], return false.

这题使用迭代的中序遍历。中序遍历是遍历左中右节点,所以对于二叉搜索树,后一个节点肯定比上一个节点大。所以根据这个思路,假设一个节点pre表示前一个节点,如果下一个节点比pre小,就返回false。中序遍历从最左边的子树开始遍历的,pre一开始就是null。当左中右遍历完,pre 在右子树上,这样继续上一层(中间的父节点)也是比pre大。

/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public boolean isValidBST(TreeNode root) {
if(root==null) return true;
Stack<TreeNode> stack=new Stack<>();
TreeNode pre=null;
while(root!=null||!stack.isEmpty()){
if(root!=null){ //将左节点依次存进栈,存完后开始遍历
stack.add(root);
root=root.left;
}else{
root=stack.pop();
if(pre!=null&&root.val<=pre.val) return false;
pre=root;
root=root.right;
}
}
return true;
}
}

下面是Binary Tree Inorder Traversal ,中序遍历二叉树非递归方法。

public List<Integer> inorderTraversal(TreeNode root) {
List<Integer> list = new ArrayList<>();
if(root == null) return list;
Stack<TreeNode> stack = new Stack<>();
while(root != null || !stack.empty()){
if(root != null){
stack.push(root);
root = root.left;
}else{
root = stack.pop();
list.add(root.val);
root = root.right;
}
}
return list;
}

Validate Binary Search Tree(一定掌握的方法)的更多相关文章

  1. 【leetcode】Validate Binary Search Tree

    Validate Binary Search Tree Given a binary tree, determine if it is a valid binary search tree (BST) ...

  2. 【LeetCode练习题】Validate Binary Search Tree

    Validate Binary Search Tree Given a binary tree, determine if it is a valid binary search tree (BST) ...

  3. LeetCode: Validate Binary Search Tree 解题报告

    Validate Binary Search Tree Given a binary tree, determine if it is a valid binary search tree (BST) ...

  4. Leetcode 笔记 98 - Validate Binary Search Tree

    题目链接:Validate Binary Search Tree | LeetCode OJ Given a binary tree, determine if it is a valid binar ...

  5. Validate Binary Search Tree

    Validate Binary Search Tree Given a binary tree, determine if it is a valid binary search tree (BST) ...

  6. LintCode Validate Binary Search Tree

    Validate Binary Search Tree Given a binary tree, determine if it is a valid binary search tree (BST) ...

  7. 39. Recover Binary Search Tree && Validate Binary Search Tree

    Recover Binary Search Tree OJ: https://oj.leetcode.com/problems/recover-binary-search-tree/ Two elem ...

  8. [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 ...

  9. leetcode dfs Validate Binary Search Tree

    Validate Binary Search Tree Total Accepted: 23828 Total Submissions: 91943My Submissions Given a bin ...

  10. 【LeetCode】98. Validate Binary Search Tree (2 solutions)

    Validate Binary Search Tree Given a binary tree, determine if it is a valid binary search tree (BST) ...

随机推荐

  1. 1082. Read Number in Chinese (25)

    题目如下: Given an integer with no more than 9 digits, you are supposed to read it in the traditional Ch ...

  2. Android的自动完成文本框-android学习之旅(二十六)

    AutoComplicatedTextView简介 AutoComplicatedTextView是从EditText派生出来的,可以使用EditText的所有的属性.当用户输入一定的内容后,会有提示 ...

  3. XML解析之sax解析案例(二)使用sax解析把 xml文档封装成对象

    Demo1类: import java.io.File; import java.util.List; import javax.xml.parsers.SAXParser; import javax ...

  4. 【翻译】使用Sencha Ext JS 6打造通用应用程序

    原文:Using Sencha Ext JS 6 to Build Universal Apps {.aligncenter} 在Sencha和整个Ext JS团队的支持下,我很高兴能跟大家分享一下有 ...

  5. 谈谈Ext JS的组件——布局的使用方法续二

    绝对布局(Ext.layout.container.Absolute) 绝对布局让我回想到了使用Foxpro开发的时候,哪时候的界面布局就是这样,通过设置控件的左上角坐标(x,y)和宽度来进行的,因为 ...

  6. (三十九)数据的持久化存储-plist实现(XML属性表)

    iOS应用数据存储的常用方式: 归档:用某种格式保存数据. XML属性列表(plist)归档(持久化) Preference 偏好设置 NSKeyedArchiver归档 SQLite3 数据库 效率 ...

  7. HTML5进阶(二)HBuilder实现软件自动升级

    HBuilder实现软件自动升级 前言 移动APP开发好后需要实现软件自动升级功能,经过一番搜索,发现HBuilder具有"App资源在线升级更新"的功能,遂研究之. 经过一番测试 ...

  8. [案例]某体育用品公司在零售领域BI的产品应用解决方案

    随着某体育用品公司集团经营规模的不断扩大,信息化的建设也在不断的深入,从POS系统到ERP系统,从MAIL系统到OA系统,整个集团的每项工作都与信息系统密不可分,可以说是行业内信息化建设的先导者.但是 ...

  9. MySQL正则表达式初步

    如果想要了解完整的MySQL手册, 请访问: MySQL 5.1参考手册 你还可以学习: MySQL学习精粹 我们知道,在SQL之中,可以用 like 这个谓词(表达式) 来进行模糊检索,并支持 %, ...

  10. 写论文如何做相关工作(realted work)的调研

    1.找一篇目标研究领域的中文综述,读懂,对该领域有了些基本的了解,如何找到好的综述,就是要关注一些大牛的实验组的综述和进展: 2.找该中文综述引用的外文文献来看,通常是一些比较经典的文献 3.找这些外 ...