Leetcode 验证二叉树的前序序列化

本文介绍了一种验证二叉树前序序列化的正确性的算法。通过对输入字符串进行解析,利用节点数量或出入度计数的方法来判断序列是否有效。文章提供了两种实现方案:一种基于节点数量统计,另一种基于出入度计数。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

验证二叉树的前序序列化

在这里插入图片描述

// 叶子节点n0 = 度为2的节点n2 + 1(二叉树的孩子只能同时出现 或 同时不出现,题中符合)
// 
// 输入: "9,3,4,#,#,1,#,#,2,#,6,#,#"
// 输出: true


// "9,3,4,#,#,1,#,#,2,#,6,#,#"
// "9,3,4,#,#,1,#,#,2,#,#"
// "9,3,4,#,#,1,#,#,#"
// "9,3,4,#,#,#,#"
// "9,3,#,#,#"
// "9,#,#"
// "#"

class Solution {
    public boolean isValidSerialization(String preorder) {
        int count = 0; // 记录'#'的个数
        for(int i = preorder.length() - 1; i >= 0; i--){
            if(preorder.charAt(i) == ',') continue;
            if(preorder.charAt(i) == '#'){
                count++;
            }else{
            	// 如果不是'#','#'总数减去2,并将该数赋值为'#',总的来说'#'数量减1
            	// 叶子节点 n0 - 1,度为2的节点 n2 - 1
                while(i >= 0 && preorder.charAt(i) != ','){
                    i--;
                }
                if(count >= 2){
                    count--;
                }else{
                    return false;
                }
            }
        }
        return count == 1;
    }
}
// 入度 = 出度
// 根节点单独判断 出度 + 2
// 非'#'节点 入度 + 1 出度 + 2
// '#'节点 入度 + 1

// 优化版
class Solution{
    public boolean isValidSerialization(String preorder) {
        int indegree = 0, outdegree = 0;
        String[] split = preorder.split(",");
        if(split[0].equals("#")){
            return split.length == 1;
        }
        for (int i = 0; i < split.length; i++) {
            if (i == 0) {
                outdegree += 2;
            } else if (split[i].equals("#")) {
                indegree += 1;
            } else {
                indegree += 1;
                outdegree += 2;
            }
            if (i != split.length - 1 && indegree >= outdegree) {
                return false;
            }
        }
        return indegree == outdegree;
    }
}

// 未优化版
class Solution{
    public boolean isValidSerialization(String preorder) {
        int inDegree = 0, outDegree = 0;
        if(preorder.charAt(0) == '#') {
        	return preorder.length() == 1;
        }
        for(int i = 0; i < preorder.length(); i++){
            if(i == 0){
                outDegree += 2;
                i++;
            }else if(preorder.charAt(i) == '#'){
                inDegree++;
                if(i == preorder.length() - 1){
                    break;
                }else{
                    i++;
                }
            }else{
                while(i < preorder.length() && preorder.charAt(i) != ','){
                    i++;
                }
                inDegree++;
                outDegree += 2;
            }
            if(i != preorder.length() - 1 && inDegree >= outDegree) {
            	return false;
            }
        }
        return inDegree == outDegree;
    }
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值