【leetcode-449】序列化和反序列化二叉搜索树

序列化是将数据结构或对象转换为一系列位的过程,以便它可以存储在文件或内存缓冲区中,或通过网络连接链路传输,以便稍后在同一个或另一个计算机环境中重建。

设计一个算法来序列化和反序列化二叉搜索树。 对序列化/反序列化算法的工作方式没有限制。 您只需确保二叉搜索树可以序列化为字符串,并且可以将该字符串反序列化为最初的二叉搜索树。

编码的字符串应尽可能紧凑。

注意:不要使用类成员/全局/静态变量来存储状态。 你的序列化和反序列化算法应该是无状态的。

思路:

普通的二叉树需要两种遍历结果才能固定二叉树,而对于BST,得到BST的前序遍历,根据BST的性质,第一个元素值为根节点,小于根节点的元素为左子树,大于根节点的元素为右子树。

class Codec {
    // Encodes a tree to a single string.
    //BST的前序遍历结果
    public String serialize(TreeNode root) {
        if (root == null) return "";
        StringBuilder sb = new StringBuilder();
        helper(root, sb);
        return sb.substring(0, sb.length() - 1);
    }

    private void helper(TreeNode root, StringBuilder sb) {
        if (root == null) return;
        //拼接当前节点
        sb.append(root.val).append(",");
        helper(root.left, sb);
        helper(root.right, sb);
    }

    // Decodes your encoded data to tree.
    public TreeNode deserialize(String data) {
        if (data == null || data.length() == 0) return null;
        String[] arr = data.split(",");
        return builder(arr, 0, arr.length - 1);
    }

    private TreeNode builder(String[] arr, int lo, int hi) {
        if (lo > hi) return null;
        TreeNode root = new TreeNode(Integer.valueOf(arr[lo]));
        //找到第一个比首元素大的元素位置,这个位置把数组分割为左右子树
        int index = hi + 1;
        for (int i = lo + 1; i <= hi; i++) {
            if (Integer.valueOf(arr[i]) > root.val) {
                index = i;
                break;
            }
        }
        //递归构建子树
        root.left = builder(arr, lo + 1, index - 1);
        root.right = builder(arr, index, hi);
        return root;
    }
}

链接:https://leetcode-cn.com/problems/serialize-and-deserialize-bst/solution/java-shi-yong-qian-xu-bian-li-jin-xing-xu-lie-hua-/
来源:力扣(LeetCode)

原文地址:https://www.cnblogs.com/twoheads/p/11706395.html

时间: 2024-08-30 04:39:46

【leetcode-449】序列化和反序列化二叉搜索树的相关文章

[LeetCode] Serialize and Deserialize BST 二叉搜索树的序列化和去序列化

Serialization is the process of converting a data structure or object into a sequence of bits so that it can be stored in a file or memory buffer, or transmitted across a network connection link to be reconstructed later in the same or another comput

LeetCode 109——有序链表转化二叉搜索树

1. 题目 2. 解答 2.1. 方法一 在 LeetCode 108--将有序数组转化为二叉搜索树 中,我们已经实现了将有序数组转化为二叉搜索树.因此,这里,我们可以先遍历一遍链表,将节点的数据存入有序数组中,然后再将有序数组转化为二叉搜索树即可. class Solution { public: TreeNode* sortedListToBST(ListNode* head) { vector<int> nums; while (head != NULL) { nums.push_bac

[LeetCode] 109. 有序链表转换二叉搜索树

题目链接 : https://leetcode-cn.com/problems/convert-sorted-list-to-binary-search-tree/ 题目描述: 给定一个单链表,其中的元素按升序排序,将其转换为高度平衡的二叉搜索树. 本题中,一个高度平衡二叉树是指一个二叉树每个节点 的左右两个子树的高度差的绝对值不超过 1. 示例: 给定的有序链表: [-10, -3, 0, 5, 9], 一个可能的答案是:[0, -3, 9, -10, null, 5], 它可以表示下面这个高

[LeetCode] Binary Search Tree Iterator 二叉搜索树迭代器

Implement an iterator over a binary search tree (BST). Your iterator will be initialized with the root node of a BST. Calling next() will return the next smallest number in the BST. Note: next() and hasNext() should run in average O(1) time and uses

LeetCode 109. 有序链表转换二叉搜索树(Convert Sorted List to Binary Search Tree)

题目描述 给定一个单链表,其中的元素按升序排序,将其转换为高度平衡的二叉搜索树. 本题中,一个高度平衡二叉树是指一个二叉树每个节点 的左右两个子树的高度差的绝对值不超过 1. 示例: 给定的有序链表: [-10, -3, 0, 5, 9], 一个可能的答案是:[0, -3, 9, -10, null, 5], 它可以表示下面这个高度平衡二叉搜索树: 0 / -3 9 / / -10 5 解题思路 转换二叉搜索树分为以下两步: 首先找到链表中间节点以及它的前一个节点,可以利用快慢指针的思想,接着以

[LeetCode] 109. 有序链表转换二叉搜索树 ☆☆☆(递归)

描述 给定一个单链表,其中的元素按升序排序,将其转换为高度平衡的二叉搜索树. 本题中,一个高度平衡二叉树是指一个二叉树每个节点 的左右两个子树的高度差的绝对值不超过 1. 示例: 给定的有序链表: [-10, -3, 0, 5, 9], 一个可能的答案是:[0, -3, 9, -10, null, 5], 它可以表示下面这个高度平衡二叉搜索树: 0 / \ -3 9 / / -10 5 解析 有序链表,平衡二叉树,很自然想到将链表一分为二,两部分就是中间节点的左右节点.递归执行左右2个部分的节点

C# Json 序列化与反序列化二

/// <summary> /// 将对象转换为 JSON 字符串 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="input"></param> /// <returns></returns> public static string ScriptSerialize<T>

二叉搜索树的实现源码(源码较长,请慎入)

实现二叉搜索树的一种好方法是利用二叉树抽象数据类型. 我们以BisTree这个名称来代表二叉搜索树这种数据结构.通过typedef方式将BisTree(二叉搜索树)实现为BiTree(二叉树)的别名. 采用typedef方法使得二叉搜索树具有了某种程度的多态能力,如同栈和队列一样.这意味着除了专属于二叉搜索树的操作外,还可以在其上执行属于二叉树的操作. 数据结构AvlNode代表树中的结点,AvlNode包含3个成员: data是存储在结点中的数据:hidden用来标识结点是否已经移除:fact

LeetCode 501. Find Mode in Binary Search Tree (找到二叉搜索树的众数)

Given a binary search tree (BST) with duplicates, find all the mode(s) (the most frequently occurred element) in the given BST. Assume a BST is defined as follows: The left subtree of a node contains only nodes with keys less than or equal to the nod