题解 | 序列化二叉树

序列化二叉树

https://www.nowcoder.com/practice/cf7e25aa97c04cc1a68c8f040e71fb84

import java.util.*;
/*
public class TreeNode {
    int val = 0;
    TreeNode left = null;
    TreeNode right = null;

    public TreeNode(int val) {
        this.val = val;

    }

}
*/
public class Solution {
    String Serialize(TreeNode root) {
        //隔离空树
        if(root==null) return "{}";
        Queue<TreeNode> queue = new LinkedList<>();
        queue.add(root);
        int height = getHeight(root);
        Queue<Integer> res = new LinkedList<>();
        //层序遍历
        while(!queue.isEmpty()){
            TreeNode curNode = queue.poll();
            
            if(curNode==null){
                res.add(-1);
            }else{
                res.add(curNode.val);
                queue.add(curNode.left);
                queue.add(curNode.right);
            }

        }
        StringBuilder resStr = new StringBuilder();
        resStr.append("{");

        int buffer = 0; 
        while(!res.isEmpty()){
            int curVal = res.poll();
            if(curVal==-1){
                buffer++;
                
            }else{
                for(int i = 0;i<buffer;i++) resStr.append("#,");
                resStr.append(curVal);
                resStr.append(",");
                buffer = 0;
            }
        }
        resStr.deleteCharAt(resStr.length()-1);
        resStr.append("}");
        System.out.println(resStr);
        return resStr.toString();
    }
    int getHeight(TreeNode root){
        if(root==null) return 0;
        int height = 1 + Math.max(getHeight(root.left),getHeight(root.right));
        return height;        
    }



    TreeNode Deserialize(String str) {

        //隔离空树
        if(str.equals("{}")) return null;

        str = str.substring(1,str.length()-1);
        System.out.println(str);
        String[] strs = str.split(",");

        Queue<String> strQueue = new LinkedList<>();

        for(String s:strs) strQueue.add(s);
        
        TreeNode root = new TreeNode(Integer.valueOf(strQueue.poll()));
        Queue<TreeNode> nodeQueue = new LinkedList<>();
        nodeQueue.add(root);

        while(!nodeQueue.isEmpty()&&!strQueue.isEmpty()){
            TreeNode curRoot = nodeQueue.poll();
            String left = strQueue.poll();
            String right = strQueue.poll();
            System.out.println(left+" "+right);
            if(left!=null&&!left.equals("#")) {
                curRoot.left = new TreeNode(Integer.valueOf(left));
                nodeQueue.add(curRoot.left);
            }
            if(right!=null&&!right.equals("#")){
                curRoot.right = new TreeNode(Integer.valueOf(right));
                nodeQueue.add(curRoot.right);
            } 
        }
        return root;
    }
}

全部评论

相关推荐

今年读完研的我无房无车无对象,月入还没有过万&nbsp;看到他在朋友圈晒房产证,感叹自己白读了这么多年书
梦想是成为七海千秋:那咋了,双9毕业的现在还没存款呢(因为没念完),高中毕业的去直播带货月入几百万也是完全有可能的,退一万步讲,有些人刚出生父母就给买车买房了,上哪说理去,哪怕是同一个起点也会有截然不同的走向,过好自己的生活就完事了。
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务