题解 | 序列化二叉树

序列化二叉树

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

全部评论

相关推荐

08-06 14:01
门头沟学院 Java
点赞 评论 收藏
分享
头像
07-24 13:05
已编辑
西南大学 Java
点赞 评论 收藏
分享
求offer的大角牛:简历写的第一乱,没有突出重点,第二项目太多太杂看不出来有啥核心技术,第三自我评价太多了,第四获得的荣誉没啥含金量,可以不写,反正问题不少
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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