力扣labuladong——一刷day73

发布时间:2023年12月17日

提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档


前言


可以直接中序遍历两个 BST 得到两个有序数组,然后把这两个有序数组合并,这个思路简单,但是空间复杂度会高一些

一、力扣449. 序列化和反序列化二叉搜索树

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

    // Encodes a tree to a single string.
    public String serialize(TreeNode root) {
        fun(root);
        return sb.toString();
    }

    // Decodes your encoded data to tree.
    public TreeNode deserialize(String data) {
        if(data.isEmpty()){
            return null;
        }
        LinkedList<Integer> list = new LinkedList<>();
        for(String s : data.split(",")){
            list.add(Integer.parseInt(s));
        }
        return fun(list,Integer.MIN_VALUE, Integer.MAX_VALUE);
    }
    public TreeNode fun(LinkedList<Integer> list, int min, int max){
        if(list.isEmpty()){
            return null;
        }
        int temp = list.getFirst();
        if(temp > max || temp < min){
            return null;
        }
        list.removeFirst();
        TreeNode cur = new TreeNode(temp);
        cur.left = fun(list, min, temp);
        cur.right = fun(list, temp, max);
        return cur;
    }
    public void fun(TreeNode root){
        if(root == null){
            return;
        }
        sb.append(root.val).append(",");
        fun(root.left);
        fun(root.right);
    }
}

// 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;

二、力扣173. 二叉搜索树迭代器

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class BSTIterator {
    List<Integer> list = new ArrayList<>();
    int index = 0;
    public BSTIterator(TreeNode root) {
        fun(root);
    }
    
    public int next() {
        int cur = list.get(index);
        index ++;
        return cur;
    }
    
    public boolean hasNext() {
        if(index < list.size()){
            return true;
        }
        return false;
    }
    public void fun(TreeNode root){
        if(root == null){
            return ;
        }
        fun(root.left);
        list.add(root.val);
        fun(root.right);
    }
}

/**
 * Your BSTIterator object will be instantiated and called as such:
 * BSTIterator obj = new BSTIterator(root);
 * int param_1 = obj.next();
 * boolean param_2 = obj.hasNext();
 */
文章来源:https://blog.csdn.net/ResNet156/article/details/134963212
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。