98. Validate Binary Search Tree

原创 2016年05月30日 09:45:19

Given a binary tree, determine if it is a valid binary search tree (BST).

题解:这里写图片描述

代码

public class Solution {
    List<Integer> list = new ArrayList<Integer>();

    public boolean isValidBST(TreeNode root) {
        //二叉查找数 如果中序遍历结果是一个递增的排序序列
        if (root == null) return true;
        if (root.left == null && root.right == null) return true;
        inOrderTraversal(root);//中序遍历
        for (int i = 1; i < list.size(); i++) {  
            if (list.get(i) <= list.get(i - 1)) return false;  
        }  
        return true;  
     }

    public void inOrderTraversal(TreeNode root){
        if (root == null) return;  
        inOrderTraversal(root.left);  
        list.add(root.val);  
        inOrderTraversal(root.right);
       }
}
版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

leetcode98---Validate Binary Search Tree

问题描述:Given a binary tree, determine if it is a valid binary search tree (BST).Assume a BST is define...
  • will130
  • will130
  • 2015年12月26日 13:15
  • 212

Leetcode 98 Validate Binary Search Tree 验证二叉查找树

给出一个二叉树,判断其是否是合法的二叉查找树。

leetcode_98_Validate Binary Search Tree

思路: 由于二叉排序树和对二叉树的中序遍历所形成的值是有序的是充分必要条件,所以仅需对二叉树进行中序遍历即可,并将遍历的结点的值存储到一个list中,然后依次比较list中的值,是有序的则二叉树为二...

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 a...

98. Validate Binary Search Tree(提交了好几次,终于.....)

Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defined...

Leetcode 98. Validate Binary Search Tree

https://leetcode.com/problems/validate-binary-search-tree/ Given a binary tree, determine if it is ...

Leetcode OJ 98 Validate Binary Search Tree [Medium]

Leetcode OJ 98 Validate Binary Search Tree 题目描述: Given a binary tree, determine if it is a valid b...

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

LeetCode98—Validate Binary Search Tree

LeetCode98—Validate Binary Search Tree判断是否是查找二叉树,三种方式。

[Leetcode 98] Validate Binary Search Tree

原题: Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defin...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:98. Validate Binary Search Tree
举报原因:
原因补充:

(最多只允许输入30个字)