序列化和還原序列化二叉樹(java版)

來源:互聯網
上載者:User

【題目描述】請實現兩個函數,分別用來序列化和還原序列化二叉樹

【解題思路1】
//1. 利用前序走訪序列化二叉樹,然後從屬記錄的字串中還原序列化二叉樹。
//2. 遇到空節點需要用特殊字元加以標記。如“#”

public class Solution {    public int index = -1;    String Serialize(TreeNode root) {        StringBuilder s = new StringBuilder();        if(root == null){            s.append("#,");            return s.toString();        }        s.append(root.val+",");        s.append(Serialize(root.left));        s.append(Serialize(root.right));        return s.toString();    }    TreeNode Deserialize(String str) {        index++;        String[] DLRseq = str.split(",");        TreeNode leave = null;        if(!DLRseq[index].equals("#")){            leave = new TreeNode(Integer.valueOf(DLRseq[index]));            leave.left = Deserialize(str);            leave.right = Deserialize(str);        }        return leave;    }  }

【解題思路2】
//1. 層序遍曆二叉樹,序列化二叉樹
//2. 空節點需要使用特殊字元記錄。將二叉樹序列化成為一顆完全二叉樹,然後還原序列化二叉樹。

public class Serialize {    String Serialize(TreeNode root) {        if(root == null){            return null;        }        StringBuffer sb = new StringBuffer();        ArrayList<TreeNode> list = new ArrayList<TreeNode>();        int count = (1 << treeDepth(root)) - 1;//計數,拿到此樹的深度後計算對應完全二叉樹節點數        list.add(root);        count--;        TreeNode tmpNode = null;        //層次遍曆二叉樹,開始序列化        while(list.size() > 0 && count >= 0){            tmpNode = list.remove(0);            if(tmpNode != null){                sb.append(tmpNode.val+",");                list.add(tmpNode.left);                list.add(tmpNode.right);            }else{                sb.append("#,");//#作為空白節點預留位置                list.add(null);                list.add(null);            }            count --;        }        return sb.toString();    }    TreeNode Deserialize(String str) {        if(str == null || str.length() == 0){            return null;        }        return Deserialize(str.split(","), 0);    }    TreeNode Deserialize(String[] strings, int index){         TreeNode newNode = null;        if(index < strings.length){            if(!strings[index].equals("#")){                newNode = new TreeNode(Integer.parseInt(strings[index]));                newNode.left = Deserialize(strings, 2 * index + 1);                newNode.right = Deserialize(strings, 2 * index + 2);            }        }        return newNode;    }    int treeDepth(TreeNode root){        int depth = 0;        if(root == null){            return depth;        }else{            int lDepth = treeDepth(root.left) + 1;            int rDepth = treeDepth(root.right) + 1;            return lDepth > rDepth ? lDepth : rDepth;        }    }}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.