四种遍历实现二叉树的序列化

package class07;
import java.util.*;

public class SerialBT {
    public static class Node{
        int value;
        Node left;
        Node right;
        public Node(int value){
            this.value=value;
        }
    }

    //pre
    public static void SerialByPre(Node head){
        Queue<String>  queue=new LinkedList<>();
        preSerial(head,queue);
    }
    public static void preSerial(Node head,Queue<String> queue){
        if(head==null){
            queue.add(null);
        }else{
        queue.add(String.valueOf(head.value));
        preSerial(head.left,queue);
        preSerial(head.right,queue);
        }
    }
    public static void inSerial(Node head,Queue<String> queue){
        if(head==null) queue.add(null);//包含了头为null《和递归的时候结束,没有返回值的递归,让最后一次执行不能有递归
        else{
            inSerial(head.left,queue);
            queue.add(String.valueOf(head.value));
            inSerial(head.right,queue);
        }
    }
    public static void posSerial(Node head,Queue<String> queue){
        if(head==null) queue.add(null);
        else{
            inSerial(head.left,queue);
            inSerial(head.right,queue);
            queue.add(String.valueOf(head.value));
        }
    }


    public static Node unPreSerial(Queue<String> queue){
        String value = queue.poll();
        if(value==null)  return null;
        Node head=new Node(Integer.valueOf(value));
        head.left= unPreSerial(queue);
        head.right=unPreSerial(queue);
        return head;
    }

//    public static Node unInSerial(Queue<String> queue){
//        String value = queue.poll();
//        if (value==null) return null;
//        node.left=unInSerial(queue);
//        Node node=new Node(Integer.valueOf(value));
//
//    }


    //后序序列化采用栈把中调到最前面
    public static void unPosSerial(Queue<String> queue){
        String value = queue.poll();
        if(value==null) return;
        Stack<String> stack=new Stack<>();
        stack.push(value);
        while(!queue.isEmpty()){
            stack.push(queue.poll());//左右中---中右左
        }
    }
    public static Node unPosSerial(Stack<String> stack){
        String value=stack.pop();
        if(value==null) return  null;
        else{
            Node head=new Node(Integer.valueOf(value));
            head.left=unPosSerial(stack);
            head.right=unPosSerial(stack);
            return head;
        }
    }



    public static void levelSerial(Node head){
      Queue<String> data=new LinkedList<>();
      if(head==null) {
          data.offer(null);
          return;
      } else {

          Queue<Node> queue = new LinkedList<>();
          queue.offer(head);
          data.offer(String.valueOf(head.value));


          while (!queue.isEmpty()) {
              head = queue.poll();
              if (head.left != null) {
                  queue.offer(head.left);
                  data.offer(String.valueOf(head.left));
              } else {
                  data.offer(null);
              }


              if (head.right != null) {
                  queue.offer(head.right);
                  data.offer(String.valueOf(head.right));
              } else {
                  data.offer(null);
              }
          }
      }
    }

    //不是递归,返回的可能是一个null
    public static Node unLevel(Queue<String> data){
        String value = data.poll();
        if(value==null){
            return null;
        }else{
            Queue<Node> queue=new LinkedList<>();
            Node head = Generate(value);
            queue.add(head);
            while(!queue.isEmpty()){
                Node cur = queue.poll();
                cur.left=Generate(data.poll());
                cur.right=Generate(data.poll());


                if(cur.left!=null) queue.add(cur.left);
                if(cur.right!=null) queue.add(cur.right);
            }
        return head;
        }
    }
    public static Node Generate(String value){
        if(value==null) return null;
        else{
            return new Node(Integer.valueOf(value));
        }
    }

}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

赑屃爱Java

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值