is validate binary search tree

来源:互联网 发布:sql基本查询语句 编辑:程序博客网 时间:2024/06/03 14:20

is validate binary search tree

public class validateBST{  static class TreeNode{  int val;    TreeNode left;    TreeNode right;    TreeNode(int x){    val=x;    }  }  static class TreeWrap{    TreeNode node;  }  public boolean isValBST(TreeNode root){  return text( root,new TreeWrap());  }  boolean text(TreeNode root,TreeWrap pre){  if(root==null)      return true;    if(!text(root.left,pre))      return false;    if(pre.node!=null&&root.val<=pre.node.val)      return false;      pre.node=root;    return text(root.right,pre);  }  public static void main(String args[]){  validateBST v=new validateBST();    TreeNode root=new TreeNode(5);    root.left=new TreeNode(4);    root.right=new TreeNode(7);    root.right.left=new TreeNode(6);    root.right.right=new TreeNode(8);    System.out.print(v.isValBST(root));  }}



原创粉丝点击