/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/**/***## /迭代法*****classSolution{publicbooleanisValidBST(TreeNode root){returndfs(root,null,null);}booleandfs(TreeNode root,Integer l ,Integer r){if(root==null)returntrue;if(l!=null&&l>=root.val)returnfalse;if(r!=null&&r<=root.val)returnfalse;returndfs(root.left,l,root.val)&&dfs(root.right,root.val,r);}}
中序遍历
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/classSolution{long pre=Long.MIN_VALUE;publicbooleanisValidBST(TreeNode root){if(root==null)returntrue;if(!isValidBST(root.left))returnfalse;if(pre>=root.val)returnfalse;
pre=root.val;returnisValidBST(root.right);}}