C语言 | Leetcode C语言题解之第331题验证二叉树的前序序列化

题目:

题解:

cpp 复制代码
bool isValidSerialization(char* preorder) {
    int n = strlen(preorder);
    int i = 0;
    int slots = 1;
    while (i < n) {
        if (!slots) {
            return false;
        }
        if (preorder[i] == ',') {
            i++;
        } else if (preorder[i] == '#') {
            slots--;
            i++;
        } else {
            // 读一个数字
            while (i < n && preorder[i] != ',') {
                i++;
            }
            slots++;  // slots = slots - 1 + 2
        }
    }
    return !slots;
}
相关推荐
晓蛋11 小时前
c语言指的是什么意思
c语言·编译器·编程开发·集成开发环境·程序实例
傲世仙尊11 小时前
目录即文件-Ext文件系统收尾篇
linux·c语言
牵猫散步的鱼儿12 小时前
重载、重写(覆盖)、重定义区别
c语言
phltxy12 小时前
C 语言指针:从内存地址到灵活的数据访问
c语言
phltxy13 小时前
C 语言中的数据存储:从类型到二进制位
c语言
旖旎夜光13 小时前
力控面试题 01.01: 判定字符是否唯一(位运算) —— 题解
c++·学习·算法·leetcode·力控
wuminyu16 小时前
HotSpot的轻量锁与膨胀后的ObjectMonitor状态重建原理
java·linux·c语言·jvm·c++
CoderYanger16 小时前
A.每日一题:835. 图像重叠
java·开发语言·程序人生·leetcode·面试·职场和发展·学习方法
圣保罗的大教堂16 小时前
leetcode 3524. 求出数组的 X 值 I 中等
leetcode
码不停蹄Zzz16 小时前
指针用法篇——malloc和free指针详解
c语言