leetcode 297. 二叉树的序列化与反序列化

题目:297. 二叉树的序列化与反序列化 - 力扣(LeetCode)

宽度有限搜索的具象化,没啥难度,注意二叉树中空节点的处理即可。

cpp 复制代码
class Codec {
public:

    // Encodes a tree to a single string.
    string serialize(TreeNode* root) {
        if (root == nullptr) {
            return "[]";
        }
        vector<TreeNode*> arr;
        arr.push_back(root);
        int i = 0;
        while (i < arr.size()) {
            TreeNode* t = arr[i];
            i++;
            if (!t) {
                continue;
            }
            arr.push_back(t->left);
            arr.push_back(t->right);
        }
        while (arr.size() && arr[arr.size() - 1] == nullptr) {
            arr.pop_back();
        }
        
        string data = "[";
        for (i = 0; i < arr.size(); i++) {
            if (!arr[i]) {
                data += "null";
            } else {
                data += to_string(arr[i]->val);
            }
            if (i < arr.size() - 1) {
                data += ",";
            } else {
                data += "]";
            }
        }
        return data;
    }

    // Decodes your encoded data to tree.
    TreeNode* deserialize(string data) {
        vector<TreeNode*> arr;
        int i = 1;
        int end = data.length() - 2;
        bool neg;
        int val = 0;
        while (i <= end) {
            if (data[i] == 'n') {
                arr.push_back(nullptr);
                i += 5;
                continue;
            }
            neg = false;
            if (data[i] == '-') {
                neg = true;
                i++;
            }
            val = 0;
            while (i <= end && data[i] != ',') {
                val = val * 10 + data[i] - '0';
                i++;
            }
            if (neg) {
                val = - val;
            }
            TreeNode* t = new TreeNode(val);
            arr.push_back(t);
            i++;
        }
        
        if (arr.empty()) {
            return nullptr;
        }
        
        int pid = 0;
        TreeNode* parent;
        for (int i = 1; i < arr.size(); i++) {
            parent = arr[pid];
            if (i % 2 == 1) {
                parent->left = arr[i];
            } else {
                parent->right = arr[i];
                pid++;
                while (!arr[pid]) {
                    pid++;
                }
            }
        }
        return arr[0];
    }
};
相关推荐
罗超驿5 小时前
2.LeetCode 1089. 复写零——双指针解法学习笔记
java·算法·leetcode
khalil10206 小时前
代码随想录算法训练营Day-41动态规划08 | 121. 买卖股票的最佳时机、122.买卖股票的最佳时机II、123.买卖股票的最佳时机III
数据结构·c++·算法·leetcode·动态规划
1104.北光c°6 小时前
Leetcode215 三种写法完成数组中的第K个最大元素 【hot100算法个人笔记】【java写法】
java·笔记·程序人生·算法·leetcode·排序算法·快速选择
浅念-7 小时前
LeetCode最短路必看:BFS算法原理+经典题解
数据结构·c++·算法·leetcode·职场和发展·bfs·宽度优先
代码地平线7 小时前
【数据结构】二叉树详解:全代码逐行解析+6道LeetCode高频OJ题图解
数据结构·算法·leetcode
田梓燊7 小时前
翻转二叉树
leetcode
流年如夢8 小时前
顺序表(LeetCode)
c语言·数据结构·leetcode·职场和发展
浅念-21 小时前
刷穿LeetCode:BFS 解决 Flood Fill 算法
数据结构·c++·算法·leetcode·职场和发展·bfs·宽度优先
im_AMBER1 天前
手撕hot100之矩阵!看完这篇就AC~
javascript·数据结构·线性代数·算法·leetcode·矩阵
沪漂阿龙1 天前
程序员面试技术爆款文:2026大厂算法通关手册——从零基础到LeetCode刷穿,这一篇就够了
算法·leetcode·面试