LeetCode 449. 序列化和反序列化二叉搜索树 (BFS \ 二叉搜索树)

LeetCode 449. 序列化和反序列化二叉搜索树 (BFS \ 二叉搜索树)_第1张图片
类似题目:297. 二叉树的序列化与反序列化

思路

一、BFS层序遍历:思路和297题一样

二、利用二叉搜索树的性质
LeetCode 449. 序列化和反序列化二叉搜索树 (BFS \ 二叉搜索树)_第2张图片

代码实现(java)

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
public class Codec {
    // 前序遍历
    public String serialize(TreeNode root) {
        if (root == null) return null;
        List<String> list = new ArrayList<>();
        dfs1(root, list);
        int n = list.size();
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < n; i++) {
            sb.append(list.get(i));
            if (i != n - 1) sb.append(",");
        }
        return sb.toString();
    }
    void dfs1(TreeNode root, List<String> list) {
        if (root == null) return ;
        list.add(String.valueOf(root.val));
        dfs1(root.left, list);
        dfs1(root.right, list);
    }
    public TreeNode deserialize(String s) {
        if (s == null) return null;
        String[] ss = s.split(",");
        return dfs2(0, ss.length - 1, ss);
    }
    TreeNode dfs2(int l, int r, String[] ss) {
        if (l > r) return null;
        int j = l + 1, t = Integer.parseInt(ss[l]);
        TreeNode ans = new TreeNode(t);
        while (j <= r && Integer.parseInt(ss[j]) <= t) j++;
        ans.left = dfs2(l + 1, j - 1, ss);
        ans.right = dfs2(j, r, ss);
        return ans;
    }



    // // BFS层序遍历 (没有利用二叉搜索树)
    // // Encodes a tree to a single string.
    // public String serialize(TreeNode root) {
    //     if(root == null) return "";

    //     StringBuilder sb = new StringBuilder();
    //     Queue queue = new LinkedList<>();
    //     queue.offer(root);

    //     while(!queue.isEmpty()) {
    //         TreeNode node = queue.poll();
    //         if(node == null) {
    //             sb.append("X").append(",");
    //             continue;
    //         }

    //         sb.append(node.val).append(",");
    //         queue.offer(node.left);
    //         queue.offer(node.right);
    //     }
    //     return sb.toString();
    // }

    // // Decodes your encoded data to tree.
    // public TreeNode deserialize(String data) {
    //     if(data == "") return null;

    //     Queue nodeQueue = new LinkedList<>(Arrays.asList(data.split(",", -1)));
    //     Queue rootQueue = new LinkedList<>();
    //     TreeNode root = new TreeNode(Integer.parseInt(nodeQueue.poll()));
    //     rootQueue.offer(root);

    //     while(!rootQueue.isEmpty()) {
    //         TreeNode rootN = rootQueue.poll();
    //         String left = nodeQueue.poll();
    //         String right = nodeQueue.poll();

    //         if(!"X".equals(left)) {
    //             rootN.left = new TreeNode(Integer.parseInt(left));
    //             rootQueue.offer(rootN.left);
    //         }
    //         if(!"X".equals(right)) {
    //             rootN.right = new TreeNode(Integer.parseInt(right));
    //             rootQueue.offer(rootN.right);
    //         }
    //     }
    //     return root;
    // }
}

// Your Codec object will be instantiated and called as such:
// Codec ser = new Codec();
// Codec deser = new Codec();
// String tree = ser.serialize(root);
// TreeNode ans = deser.deserialize(tree);
// return ans;

你可能感兴趣的:(算法,leetcode算法,leetcode,宽度优先,深度优先,java,算法)