{"id":139973,"date":"2020-07-15T21:12:15","date_gmt":"2020-07-15T13:12:15","guid":{"rendered":"http:\/\/4563.org\/?p=139973"},"modified":"2020-07-15T21:12:15","modified_gmt":"2020-07-15T13:12:15","slug":"%e5%8a%9b%e6%89%a3-%e9%a2%86%e6%89%a3%e7%ae%97%e6%b3%95%e9%a2%98%e8%a7%a3-%e5%ba%8f%e5%88%97%e5%8c%96-trie-%c2%b7-trie-serialization","status":"publish","type":"post","link":"http:\/\/4563.org\/?p=139973","title":{"rendered":"[\u529b\u6263\/\u9886\u6263\u7b97\u6cd5\u9898\u89e3] \u5e8f\u5217\u5316 Trie \u00b7 Trie Serialization"},"content":{"rendered":"<div>\n<div>\n<div>\n<h1>                  [\u529b\u6263\/\u9886\u6263\u7b97\u6cd5\u9898\u89e3] \u5e8f\u5217\u5316 Trie \u00b7 Trie Serialization               <\/h1>\n<p> <\/p>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : zzzrf <\/span>  <span><i><\/i> 4<\/span> <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<div isfirst=\"1\"> <\/p>\n<p>[\u9898\u76ee\u63cf\u8ff0]<\/p>\n<p>\u7ed9\u4f60\u4e00\u68f5\u5b57\u5178\u6811\uff0c\u8bbe\u8ba1\u4e00\u79cd\u5e8f\u5217\u5316\u7684\u65b9\u5f0f\uff0c\u5e76\u4e14\u540c\u65f6\u8981\u80fd\u591f\u8bbe\u8ba1\u5bf9\u5e94\u7684\u5c55\u5f00\u65b9\u6cd5\u3002<\/p>\n<p><strong>\u5728\u7ebf\u8bc4\u6d4b\u5730\u5740:<\/strong><\/p>\n<p>https:\/\/www.lintcode.com\/problem\/trie-serialization\/?utm_source=sc-v2ex-fks<\/p>\n<p>Example 1<\/p>\n<pre><code>Input: &lt;a&lt;b&lt;e&lt;&gt;&gt;c&lt;&gt;d&lt;f&lt;&gt;&gt;&gt;&gt; Output: &lt;a&lt;b&lt;e&lt;&gt;&gt;c&lt;&gt;d&lt;f&lt;&gt;&gt;&gt;&gt; Explanation: The trie is look like this.      root       \/      a    \/ |    b  c  d  \/        e         f <\/code><\/pre>\n<p>Example 2<\/p>\n<pre><code>Input: &lt;a&lt;&gt;&gt; Output: &lt;a&lt;&gt;&gt; <\/code><\/pre>\n<p>[\u9898\u89e3]<\/p>\n<pre><code>class Solution {     \/**      * This method will be invoked first, you should design your own algorithm       * to serialize a trie which denote by a root node to a string which      * can be easily deserialized by your own \"deserialize\" method later.      *\/     public String serialize(TrieNode root) {         \/\/ Write your code here         if (root == null)             return \"\";          StringBuffer sb = new StringBuffer();         sb.append(\"&lt;\");         Iterator iter = root.children.entrySet().iterator();          while (iter.hasNext()) {             Map.Entry entry = (Map.Entry)iter.next();              Character key = (Character)entry.getKey();              TrieNode child = (TrieNode)entry.getValue();             sb.append(key);             sb.append(serialize(child));         }         sb.append(\"&gt;\");         return sb.toString();     }      \/**      * This method will be invoked second, the argument data is what exactly      * you serialized at method \"serialize\", that means the data is not given by      * system, it's given by your own serialize method. So the format of data is      * designed by yourself, and deserialize it here as you serialize it in       * \"serialize\" method.      *\/     public TrieNode deserialize(String data) {         \/\/ Write your code here         if (data == null || data.length() == 0)             return null;          TrieNode root = new TrieNode();         TrieNode current = root;         Stack&lt;TrieNode&gt; path = new Stack&lt;TrieNode&gt;();         for (Character c : data.toCharArray()) {             switch (c) {             case '&lt;':                 path.push(current);                 break;             case '&gt;':                 path.pop();                 break;             default:                 current = new TrieNode();                 path.peek().children.put(c, current);             }         }         return root;     } <\/code><\/pre>\n<p>[\u66f4\u591a\u8bed\u8a00\u4ee3\u7801\u53c2\u8003]<\/p>\n<p>https:\/\/www.jiuzhang.com\/solution\/trie-serialization\/?utm_source=sc-v2ex-fks<\/p>\n<\/p><\/div>\n<div> <b>\u5927\u4f6c\u6709\u8a71\u8aaa<\/b> (<span>0<\/span>)        <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<ul>\n<li>\n","protected":false},"excerpt":{"rendered":"<p>[\u529b\u6263\/\u9886\u6263\u7b97\u6cd5\u9898\u89e3] \u5e8f\u5217\u5316 T&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[],"tags":[],"_links":{"self":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/139973"}],"collection":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=139973"}],"version-history":[{"count":0,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/139973\/revisions"}],"wp:attachment":[{"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=139973"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=139973"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=139973"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}