剑指 Offer 37. 序列化二叉树

前序遍历版(牛客版):

public class Solution {
    StringBuilder path=new StringBuilder();
    String Serialize(TreeNode root) {
        preOrder(root);
        return path.toString();
        
  }
    void preOrder(TreeNode root){
        if(root==null){
            path.append("#!");
            return;
        }
        path.append(root.val).append("!");
        //path.append(String.valueOf(root.val)).append("!");
        preOrder(root.left);
        preOrder(root.right);
        
    }
    int pos=-1;
    TreeNode dePreOrder(String[] s){
        if(pos==s.length)return null;
        pos++;
        if(s[pos].equals("#"))
            return null;
        TreeNode node=new TreeNode(Integer.parseInt(s[pos]));
        node.left=dePreOrder(s);
        node.right=dePreOrder(s);
        return node;
    }
    TreeNode Deserialize(String str) {
       String[] s=str.split("!");
       return dePreOrder(s);
  }
}

层序遍历(leetcode版):

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
public class Codec {
    
    // Encodes a tree to a single string.
    public String serialize(TreeNode root) {
        if(root==null) return Arrays.toString(new int[0]);
        LinkedList<TreeNode> que=new LinkedList<>();
        que.add(root);
        StringBuilder str=new StringBuilder();
        str.append("[");
        while(!que.isEmpty()){
            TreeNode node=que.removeFirst();
            if(node==null)
              {
                  str.append("null,");
                  continue;
              }
            str.append(node.val).append(",");
            que.add(node.left);
            que.add(node.right);

        }
        str.deleteCharAt(str.length()-1);
        str.append("]");
        return str.toString();
    }

    // Decodes your encoded data to tree.
    public TreeNode deserialize(String data) {
        if(data.equals("[]")) return null;
        String res1=data.substring(1,data.length()-1);
        String[] res2=res1.split(",");//这个用法第一次见
        
        LinkedList<TreeNode> que=new LinkedList<>();
        int pos=0;
        TreeNode node=new TreeNode(Integer.parseInt(res2[pos++]));//!!!Integer中的方法将String-->int
        que.add(node);
        //队列的作用是什么?
        while(!que.isEmpty()){
            TreeNode tmp=que.removeFirst();//取出队头,他的孩子就是队列的新队头和第二个节点,但要求同一层节点应同时入队--这一点是data数组给定的,就是层序遍历的次序
            if(res2[pos].equals("null"))
                 pos++;
            else{
                 tmp.left=new TreeNode(Integer.parseInt(res2[pos]));
                 que.add(tmp.left);
                 pos++;
            }
                 
            if(res2[pos].equals("null"))
                 pos++;
            else{
                 tmp.right=new TreeNode(Integer.parseInt(res2[pos]));
                 que.add(tmp.right);
                 pos++;
            }
        }
        return node;
        
    }
}

// Your Codec object will be instantiated and called as such:
// Codec codec = new Codec();
// codec.deserialize(codec.serialize(root));
上一篇:《冰河的渗透实战笔记》电子书,442页,37万字,正式发布!!


下一篇:京东 37 岁程序员加班猝死?当事人回应:我还在写代码,已报警~