Middle-题目117:98. Validate Binary Search Tree

题目原文:
Given a binary tree, determine if it is a valid binary search tree (BST).
题目大意:
给出一个二叉树,判断是不是合法的二叉搜索树。
题目分析:
看中序遍历是否递增。
源码:(language:java)

public class Solution {
    public boolean isValidBST(TreeNode root) {
        List<Integer> list = new ArrayList<Integer>();
        inOrderVisit(list,root);
        return isSortedList(list);
    }
    private void inOrderVisit(List<Integer> list, TreeNode node) {
        if(node!=null) {
            inOrderVisit(list, node.left);
            list.add(node.val);
            inOrderVisit(list, node.right);
        }
    }
    private boolean isSortedList(List<Integer> list) {
        if(list.size()<2)
            return true;
        else {
            for(int i = 0;i<list.size()-1;i++) {
                if(list.get(i)>=list.get(i+1))
                    return false;
            }
            return true;
        }
    }
}

成绩:
4ms,beats 12.79%,众数1ms,72.10%

你可能感兴趣的:(Middle-题目117:98. Validate Binary Search Tree)