剑指 Offer 37. 序列化二叉树

题目

请实现两个函数,分别用来序列化和反序列化二叉树。

示例:
在这里插入图片描述

解题思路

序列化:其实就是把二叉树用字符串表示出来,可以考虑使用二叉树的遍历:前序,中序,后序,层次
这里我们使用前序遍历,即根左右的顺序

  • 创建一个list,用来保存前序遍历的结果
  • 前序遍历
    • 判断当前是否为空,为空则添加null字符串,直接返回
    • 添加当前节点的值
    • 递归左子树
    • 递归右子树
  • 把list转换成字符串返回

反序列化:其实就是依据字符串创建二叉树,使用和序列化时相同的方式即可

  • 对字符串进行校验
  • 把字符串切割,创建一个字符串队列
  • 前序遍历
    • 判断当前队列是否为空,为空直接返回null
    • 出队一个元素,如果为字符串null,直接返回null
    • 如果不是,则构建当前节点
    • 递归构建左子树
    • 递归构建右子树
    • 返回当前节点
  • 返回当前节点

代码

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
public class Codec {

    // Encodes a tree to a single string.
    public String serialize(TreeNode root) {
        // 创建一个list,用来存储序列化结果
        List<String> list = new ArrayList<>();
        dfs(root, list);
        return String.join(",", list);
    }

    private void dfs(TreeNode node, List<String> list) {
        // 递归结束条件
        if (node == null) {
            list.add("null");
            return;
        }

        // 二叉树的先序遍历,根左右
        list.add(String.valueOf(node.val));
        dfs(node.left, list);
        dfs(node.right, list);
    }

    // Decodes your encoded data to tree.
    public TreeNode deserialize(String data) {
        // 字符串校验
        if (data == null || data.isEmpty()) {
            return null;
        }

        //把字符串切割,转换成一个队列
        LinkedList<String> list = new LinkedList<>(Arrays.asList(data.split(",")));
        return dfs(list);
    }

    private TreeNode dfs(LinkedList<String> list) {
        // 当前队列为空时,直接返回null
        if (list.isEmpty()) {
            return null;
        }

        // 出队
        String s = list.poll();
        // 约定的空节点字符串
        if (s.equals("null")) {
            return null;
        }

        // 构建也是使用先序遍历的顺序
        TreeNode node = new TreeNode(Integer.parseInt(s));
        node.left = dfs(list);
        node.right = dfs(list);
        return node;
    }
}

// Your Codec object will be instantiated and called as such:
// Codec codec = new Codec();
// codec.deserialize(codec.serialize(root));

复杂度

  • 时间复杂度:O(n),极端情况下,二叉树退化成链表,前序遍历递归n层
  • 空间复杂度:O(n),使用list存储前序遍历中间结果
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
树的存储与遍历: 1.初始化二叉树 ```c++ #include <iostream> using namespace std; struct TreeNode { int val; TreeNode* left; TreeNode* right; TreeNode(int x) : val(x), left(NULL), right(NULL) {} }; TreeNode* createTree() { int val; cin >> val; if (val == -1) { return NULL; } TreeNode* root = new TreeNode(val); root->left = createTree(); root->right = createTree(); return root; } ``` 2.先序遍历二叉树 ```c++ void preOrder(TreeNode* root) { if (root == NULL) { return; } cout << root->val << " "; preOrder(root->left); preOrder(root->right); } ``` 3.中序遍历二叉树 ```c++ void inOrder(TreeNode* root) { if (root == NULL) { return; } inOrder(root->left); cout << root->val << " "; inOrder(root->right); } ``` 4.后序遍历二叉树 ```c++ void postOrder(TreeNode* root) { if (root == NULL) { return; } postOrder(root->left); postOrder(root->right); cout << root->val << " "; } ``` 5.销毁二叉树 ```c++ void destroyTree(TreeNode* root) { if (root == NULL) { return; } destroyTree(root->left); destroyTree(root->right); delete root; } ``` 二叉树的复原: 1.由前序、中序序列确定复原二叉树 ```c++ TreeNode* buildTree(vector<int>& preorder, vector<int>& inorder) { if (preorder.empty() || inorder.empty()) { return NULL; } int rootVal = preorder[0]; TreeNode* root = new TreeNode(rootVal); vector<int>::iterator it = find(inorder.begin(), inorder.end(), rootVal); int leftSize = it - inorder.begin(); vector<int> leftPreorder(preorder.begin() + 1, preorder.begin() + 1 + leftSize); vector<int> leftInorder(inorder.begin(), it); vector<int> rightPreorder(preorder.begin() + 1 + leftSize, preorder.end()); vector<int> rightInorder(it + 1, inorder.end()); root->left = buildTree(leftPreorder, leftInorder); root->right = buildTree(rightPreorder, rightInorder); return root; } ``` 2.由中序、后序序列确定复原二叉树 ```c++ TreeNode* buildTree(vector<int>& inorder, vector<int>& postorder) { if (inorder.empty() || postorder.empty()) { return NULL; } int rootVal = postorder.back(); TreeNode* root = new TreeNode(rootVal); vector<int>::iterator it = find(inorder.begin(), inorder.end(), rootVal); int leftSize = it - inorder.begin(); vector<int> leftInorder(inorder.begin(), it); vector<int> leftPostorder(postorder.begin(), postorder.begin() + leftSize); vector<int> rightInorder(it + 1, inorder.end()); vector<int> rightPostorder(postorder.begin() + leftSize, postorder.end() - 1); root->left = buildTree(leftInorder, leftPostorder); root->right = buildTree(rightInorder, rightPostorder); return root; } ```
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值