天梯赛 L2-004 这是二叉搜索树吗?java

java 复制代码
import java.util.Scanner;

public class Main {
    static class Node {
        int val;
        Node left, right;
        Node(int x) { val = x; }
    }

    // 构建BST或其镜像,isMirror=true表示镜像
    private static Node buildBST(int[] pre, int start, int end, boolean isMirror) {
        if (start > end) return null;
        Node root = new Node(pre[start]);
        if (start == end) return root;

        int split = start + 1;
        if (!isMirror) {
            // 普通BST规则:左子树 < 根,右子树 >= 根
            while (split <= end && pre[split] < root.val) {
                split++;
            }
            // 验证右子树所有节点都 >= 根
            for (int i = split; i <= end; i++) {
                if (pre[i] < root.val) return null;
            }
        } else {
            // 镜像BST规则:左子树 >= 根,右子树 < 根
            while (split <= end && pre[split] >= root.val) {
                split++;
            }
            // 验证右子树所有节点都 < 根
            for (int i = split; i <= end; i++) {
                if (pre[i] >= root.val) return null;
            }
        }

        // 递归构建左右子树
        root.left = buildBST(pre, start + 1, split - 1, isMirror);
        if (root.left == null && (start + 1 <= split - 1)) return null;
        root.right = buildBST(pre, split, end, isMirror);
        if (root.right == null && (split <= end)) return null;
        return root;
    }

    // 后序遍历
    private static void postOrder(Node root, StringBuilder sb) {
        if (root == null) return;
        postOrder(root.left, sb);
        postOrder(root.right, sb);
        if (sb.length() > 0) sb.append(" ");
        sb.append(root.val);
    }

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int N = sc.nextInt();
        int[] pre = new int[N];
        for (int i = 0; i < N; i++) {
            pre[i] = sc.nextInt();
        }
        sc.close();

        Node root = buildBST(pre, 0, N - 1, false);
        boolean isNormalBST = root != null;

        if (!isNormalBST) {
            root = buildBST(pre, 0, N - 1, true);
        }

        if (root != null) {
            System.out.println("YES");
            StringBuilder sb = new StringBuilder();
            postOrder(root, sb);
            System.out.println(sb);
        } else {
            System.out.println("NO");
        }
    }
}
相关推荐
智者知已应修善业5 分钟前
【51单片机8个LED,已经使用了D1D2,怎么样在不动D1D2的前提下实现D6~D8的流水灯】2024-1-19
c++·经验分享·笔记·算法·51单片机
Evand J6 分钟前
【MATLAB例程】自适应渐消扩展卡尔曼滤波(AFEKF)三维雷达目标跟踪|效果已调优,附下载链接和运行结果,代码直接运行即可
开发语言·算法·matlab·目标跟踪·卡尔曼滤波·自适应滤波·代码定制
爱装代码的小瓶子7 分钟前
3. 设计buffer模块
linux·服务器·开发语言·c++·php
郝学胜-神的一滴7 分钟前
Qt 高级开发 027: QTabWidget自定义样式表美化实战
开发语言·c++·qt·程序人生·软件构建·用户界面
keykey6.8 分钟前
迁移学习实战:用预训练模型做图像分类
开发语言·人工智能·深度学习·机器学习
双河子思8 分钟前
《代码整洁之道》——读书笔记(持续更新)
开发语言·c++·c#
川冰ICE9 分钟前
JavaScript实战②|电商网站交互效果,轮播图与购物车
开发语言·javascript·交互
listhi52011 分钟前
基于 Qt 5.8.0 的串口调试助手
开发语言·qt
sugar__salt21 分钟前
Bun 新一代 JavaScript/TypeScript 运行时:从入门到实战
开发语言·javascript·typescript
无心水21 分钟前
【OpenClaw:赚钱】案例19、内容产量5倍、广告收入翻4倍:播客转多平台内容矩阵全自动化实战(OpenAI Whisper + Claude)
java·人工智能·python·ai编程·openclaw·养龙虾·java.time