判定满二叉树是否为二叉搜索树(Case通过率只有80%)
import java.util.*;
public class Main{
static class TreeNode{
int val;
TreeNode left;
TreeNode right;
TreeNode(int val){
this.val =val;
this.left = left;
this.right = right;
}
}
public static void main(String[] args){
Scanner scanner = new Scanner(System.in);
String numStr = scanner.nextLine();
String[] strArr = numStr.split(",");
int len = strArr.length;
if(strArr[0].equals("None")){
System.out.println("True");
return;
}
int[] numArr = new int[len];
for(int i=0; i<len; i++){
numArr[i] = Integer.parseInt(strArr[i]);
}
TreeNode tree = construct(numArr, 0);
boolean result = isSearch(tree);
if(result==true){
System.out.println("True");
}
else{
System.out.println("False");
}
}
public static TreeNode construct(int[] numArr, int index){
if(numArr==null)
return null;
if(numArr.length==1){
TreeNode root = new TreeNode(numArr[0]);
return root;
}
TreeNode root = null;
if(index<numArr.length){
int value = numArr[index];
root = new TreeNode(value);
root.left = construct(numArr, 2*index+1);
root.right = construct(numArr, 2*index+2);
return root;
}
return root;
}
// 下面的做法错在没有管节点和祖先节点之间的关系,
// 只考虑了当前节点和其左右子节点之间的关系
/*
public static boolean isSearch(TreeNode root){
if(root==null)
return true;
if(root.left==null&&root.right==null)
return true;
if(root.left==null&&root.right!=null){
if(root.right.val<=root.val)
return false;
}
if(root.left!=null&&root.right==null){
if(root.left.val>=root.val)
return false;
}
if(root.left!=null&&root.right!=null){
if(root.left.val>=root.val||root.right.val<=root.val)
return false;
}
return isSearch(root.left)&&isSearch(root.right);
}
*/
public class Main{
static class TreeNode{
int val;
TreeNode left;
TreeNode right;
TreeNode(int val){
this.val =val;
this.left = left;
this.right = right;
}
}
public static void main(String[] args){
Scanner scanner = new Scanner(System.in);
String numStr = scanner.nextLine();
String[] strArr = numStr.split(",");
int len = strArr.length;
if(strArr[0].equals("None")){
System.out.println("True");
return;
}
int[] numArr = new int[len];
for(int i=0; i<len; i++){
numArr[i] = Integer.parseInt(strArr[i]);
}
TreeNode tree = construct(numArr, 0);
boolean result = isSearch(tree);
if(result==true){
System.out.println("True");
}
else{
System.out.println("False");
}
}
public static TreeNode construct(int[] numArr, int index){
if(numArr==null)
return null;
if(numArr.length==1){
TreeNode root = new TreeNode(numArr[0]);
return root;
}
TreeNode root = null;
if(index<numArr.length){
int value = numArr[index];
root = new TreeNode(value);
root.left = construct(numArr, 2*index+1);
root.right = construct(numArr, 2*index+2);
return root;
}
return root;
}
// 下面的做法错在没有管节点和祖先节点之间的关系,
// 只考虑了当前节点和其左右子节点之间的关系
/*
public static boolean isSearch(TreeNode root){
if(root==null)
return true;
if(root.left==null&&root.right==null)
return true;
if(root.left==null&&root.right!=null){
if(root.right.val<=root.val)
return false;
}
if(root.left!=null&&root.right==null){
if(root.left.val>=root.val)
return false;
}
if(root.left!=null&&root.right!=null){
if(root.left.val>=root.val||root.right.val<=root.val)
return false;
}
return isSearch(root.left)&&isSearch(root.right);
}
*/
//正确的做法
public static boolean isSearch(TreeNode root){
LinkedList<Integer> list = new LinkedList<>();
midOrder(root, list);
for(int i=1; i<list.size(); i++){
if(list.get(i-1)>=list.get(i)){
return false;
}
}
return true;
}
public static void midOrder(TreeNode root, LinkedList list){
if(root==null)
return;
midOrder(root.left, list);
list.add(root.val);
midOrder(root.right, list);
}
}
public static boolean isSearch(TreeNode root){
LinkedList<Integer> list = new LinkedList<>();
midOrder(root, list);
for(int i=1; i<list.size(); i++){
if(list.get(i-1)>=list.get(i)){
return false;
}
}
return true;
}
public static void midOrder(TreeNode root, LinkedList list){
if(root==null)
return;
midOrder(root.left, list);
list.add(root.val);
midOrder(root.right, list);
}
}
感谢楼下大佬的讲解(手动点赞)