[Leetcode] Validate Binary Search Tree (Java)

来源:互联网 发布:四海认证淘宝渔具钓箱 编辑:程序博客网 时间:2024/06/09 18:18

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.
判断给定树是否是二叉搜索树
树的题,递归吧
public class Solution {       private boolean check(TreeNode root,int min,int max) {if(root==null)return true;return root.val>min&&root.val<max&&check(root.left, min, root.val)&&check(root.right, root.val, max);}public boolean isValidBST(TreeNode root) {return check(root, Integer.MIN_VALUE, Integer.MAX_VALUE);}}


0 0
原创粉丝点击