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;
}
相关推荐
wabs6662 小时前
关于动态规划【力扣1035.不相交的线和53.最大子数组和的思考】
算法·leetcode·动态规划
退休倒计时2 小时前
【每日一题】LeetCode 199. 二叉树的右视图 TypeScript
算法·leetcode·typescript
王老师青少年编程2 小时前
2026年6月GESP真题及题解(C++二级):完全平方数计数
c++·题解·真题·gesp·二级·2026年6月·完全平方数计数
硕风和炜3 小时前
【LeetCode: 1301. 最大得分的路径数目 + DP】
java·算法·leetcode·动态规划·dp·记忆化搜索
文祐4 小时前
C语言用双向链表实现单调递减(递增)队列
c语言·开发语言·链表
剑挑星河月4 小时前
94.二叉树的中序遍历
java·算法·leetcode
Sw1zzle4 小时前
算法入门(三):二分查找 - 基础模板 & 边界控制(Leetcode 704/35/278/34/69/367)
算法·leetcode
gugucoding5 小时前
28. 【C语言】通用数据操作:`void *` 与类型无关编程
c语言·开发语言
凉、介6 小时前
ARMv8 架构下的刷 Cache 操作
c语言·笔记·学习·架构·嵌入式·arm
剑挑星河月8 小时前
234. 回文链表
java·数据结构·算法·leetcode·链表