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;
}
相关推荐
橘子汽水16839 分钟前
Leetcode 230,98:二叉搜索树中第K小的元素,验证二叉搜索树
算法·leetcode·职场和发展
天空'之城42 分钟前
C 语言工业级通用组件手写 21:限幅消抖滤波
c语言·嵌入式算法·工业级组件·限幅消抖滤波
Aurorar0rua44 分钟前
CS50 x 2024 Notes Algorithms - 07
c语言·开发语言·学习方法
神罚天下ET1 小时前
典型arm位单片机启动流程(从上电到main.c)
c语言·arm开发·单片机
普通攻击往后拉3 小时前
Leetcode 448. 找到所有数组中消失的数字
算法·leetcode·职场和发展
mifengxing3 小时前
LeetCode 189 轮转数组|3种解法拆解,从暴力到O(1)原地最优解
数据结构·算法·leetcode
noipp5 小时前
推荐题目:洛谷 P3726 [AHOI2017/HNOI2017] 抛硬币
c语言·数据结构·c++·算法·编程·洛谷·luogu
c238565 小时前
C/C++每日一练14
c语言·c++·算法
程序员-珍5 小时前
力扣 877. 石子游戏
算法·leetcode·游戏
Rabitebla5 小时前
C++ STL 之 set 详解:从底层红黑树到实际应用
开发语言·数据结构·c++·算法·leetcode