判定满二叉树是否为二叉搜索树(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 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);
    }
}
感谢楼下大佬的讲解(手动点赞)
#Java#
全部评论
刚才写了一波,发现写错了,囧。
点赞
送花
回复
分享
发布于 2019-08-28 18:14
你这个思路感觉不对,你是判断左右节点是否小于/大于当前节点就递归判断了,这种方法没有管节点和祖先节点的关系啊。第一层4  第二层2 6 第三层1 10 5 7。10大于2,符合递归判断的情况,但同时也大于了根节点4。你的算法似乎处理不了这种情况。 应该用中序遍历的思路,左根右。中序遍历过程中记录前一节点的数值,当前节点大于中序遍历前一节点的数值,就是合法的二叉搜索树。
点赞
送花
回复
分享
发布于 2019-08-28 18:15
网易互娱
校招火热招聘中
官网直投

相关推荐

点赞 评论 收藏
转发
点赞 1 评论
分享
牛客网
牛客企业服务