Validate Binary Search Tree,判断是否是二叉排序树

算法分析:两种方法,一种是中序遍历,然后得到一个序列,看序列是否是有序的。第二种,是用递归。

中序遍历:

public class Solution {
    List<Integer> list = new ArrayList<>();
    public boolean isValidBST(TreeNode root) {
        if(root == null)
        {
            return true;
        }
        inorderTraversal(root);
        for(int i = 0; i < list.size() - 1; 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);
    }
}

递归:

public class Solution {
    List<Integer> list = new ArrayList<>();
    public boolean isValidBST(TreeNode root) {
        if(root == null)
        {
            return true;
        }
        inorderTraversal(root);
        for(int i = 0; i < list.size() - 1; 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);
    }
}
时间: 2024-11-17 14:45:18

Validate Binary Search Tree,判断是否是二叉排序树的相关文章

【LeetCode】98. Validate Binary Search Tree -判断是否为二叉排序树

一.描述: 二.思路: 二叉排序树(BST),中序遍历的结果一定是非递减序列(来自百度百科): 本题中对于BST的定义是要么大于,要么小与,即遍历结果只能是递增序列,故可以通过判断中序遍历的结果序列是否是递增序列,来判断是否为合法BST: 另一种方法是使用递归: 三.代码: 1.非递归,通过中序遍历结果判断: /** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left;

LeetCode 98 Validate Binary Search Tree判断是否为合法二叉树

1 /** 2 * Definition for a binary tree node. 3 * struct TreeNode { 4 * int val; 5 * TreeNode *left; 6 * TreeNode *right; 7 * TreeNode(int x) : val(x), left(NULL), right(NULL) {} 8 * }; 9 */ 10 class Solution { 11 public: 12 bool Left(TreeNode* left,

[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

[Lintcode] Validate Binary Search Tree

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 c

LeetCode: Validate Binary Search Tree 解题报告

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 co

【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

LeetCode :: Validate Binary Search Tree[详细分析]

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 b

LeetCode: Validate Binary Search Tree [098]

[题目] 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

leetcode dfs Validate Binary Search Tree

Validate Binary Search Tree Total Accepted: 23828 Total Submissions: 91943My Submissions 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

[Leetcode][BST][Validate Binary Search Tree]

判断一颗树是不是二分查找树,非常经典基础的一个算法. 我很久之前第一次做的时候,是先求出来了树的前序遍历的结果,然后判断这个数组排序后是否和排序前相同,还要判断重复虾米的,很纠结的一种做法. 后来思考了一下怎么用递归的思路做,觉得应该根据定义返回两个子树的最大值和最小值,写了一会代码,发现好麻烦,不太对的样子. 后来看了题解,发现是用了一种反向的思维,把上下界从树的顶端传下去,而不是自下而上的约束.作者太机智了. 1 /** 2 * Definition for binary tree 3 *