• 剑指 Offer 37. 序列化二叉树


    剑指 Offer 37. 序列化二叉树

    先序遍历

    public class Codec {
    
        // Encodes a tree to a single string.
        public String serialize(TreeNode root) {
            if(root == null) return "null";
            String left = serialize(root.left);
            String right = serialize(root.right);
            return root.val + "," + left + "," + right;
        }
    
        // Decodes your encoded data to tree.
        public TreeNode deserialize(String data) {
            Deque<String> dq = new ArrayDeque<>(Arrays.asList(data.split(",")));
            return deserialize(dq);
        }
    
        TreeNode deserialize(Deque<String> dq) {
            String cur = dq.poll();
            if(cur.equals("null")) return null;
    
            TreeNode root = new TreeNode(Integer.valueOf(cur));
            root.left = deserialize(dq);
            root.right = deserialize(dq);
    
            return root;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27

    层序遍历

    public class Codec {
    
        // Encodes a tree to a single string.
        public String serialize(TreeNode root) {
            if(root == null) return "null";
    
            StringBuilder sb = new StringBuilder();
            Deque<TreeNode> queue = new LinkedList<>();
            queue.offer(root);
            while(!queue.isEmpty()){
                TreeNode cur = queue.poll();
                if(cur == null){
                    sb.append("null,");
                    continue;
                }
                sb.append(cur.val + ",");
                queue.offer(cur.left);
                queue.offer(cur.right);
            }
    
            return sb.toString();
        }
    
        // Decodes your encoded data to tree.
        public TreeNode deserialize(String data) {
            if(data.equals("null")) return null;
    
            Deque<String> q1 = new ArrayDeque<>(Arrays.asList(data.split(",")));
            TreeNode root = new TreeNode(Integer.valueOf(q1.poll()));
            Deque<TreeNode> q2 = new ArrayDeque<>();
            q2.offer(root);
            
            while(!q1.isEmpty()){
                TreeNode cur = q2.poll();
                String left = q1.poll();
                String right = q1.poll();
    
                if(!left.equals("null")){
                    cur.left = new TreeNode(Integer.valueOf(left));
                    q2.offer(cur.left);
                } 
                if(!right.equals("null")){
                    cur.right = new TreeNode(Integer.valueOf(right));
                    q2.offer(cur.right);
                } 
            }
    
            return root;
        }
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
  • 相关阅读:
    Web----网络通讯部分
    JS 通过年份获取月,季度,半年度,年度
    整理了一个前端utils工具方法库
    五、C语言判断语句
    带你进入桌面数控机床金工实训室
    【前端】命令行基础,linux常用命令
    [Servlet 4]Bean与DAO设计模式
    Unity UI自适应
    LINUX 网络管理
    STM32笔记—定时器
  • 原文地址:https://blog.csdn.net/m0_46283220/article/details/132905143