{"id":150664,"date":"2020-09-03T01:51:32","date_gmt":"2020-09-02T17:51:32","guid":{"rendered":"http:\/\/4563.org\/?p=150664"},"modified":"2020-09-03T01:51:32","modified_gmt":"2020-09-02T17:51:32","slug":"%e5%b7%a8%e7%a1%ac%e5%ae%b6%e9%9d%a2%e8%af%95%e9%a2%98%ef%bc%9a%e4%ba%8c%e5%8f%89%e6%a0%91%e7%9a%84%e9%94%af%e9%bd%bf%e5%bd%a2%e5%b1%82%e6%ac%a1%e9%81%8d%e5%8e%86","status":"publish","type":"post","link":"http:\/\/4563.org\/?p=150664","title":{"rendered":"\u5de8\u786c\u5bb6\u9762\u8bd5\u9898\uff1a\u4e8c\u53c9\u6811\u7684\u952f\u9f7f\u5f62\u5c42\u6b21\u904d\u5386"},"content":{"rendered":"<div>\n<div>\n<div>\n<h1>                  \u5de8\u786c\u5bb6\u9762\u8bd5\u9898\uff1a\u4e8c\u53c9\u6811\u7684\u952f\u9f7f\u5f62\u5c42\u6b21\u904d\u5386               <\/h1>\n<p> <\/p>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : zzzrf <\/span>  <span><i><\/i> 18<\/span> <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<div isfirst=\"1\"> <\/p>\n<p>\u7ed9\u51fa\u4e00\u68f5\u4e8c\u53c9\u6811\uff0c\u8fd4\u56de\u5176\u8282\u70b9\u503c\u7684\u952f\u9f7f\u5f62\u5c42\u6b21\u904d\u5386\uff08\u5148\u4ece\u5de6\u5f80\u53f3\uff0c\u4e0b\u4e00\u5c42\u518d\u4ece\u53f3\u5f80\u5de6\uff0c\u5c42\u4e0e\u5c42\u4e4b\u95f4\u4ea4\u66ff\u8fdb\u884c\uff09<\/p>\n<p>\u5728\u7ebf\u505a\u9898\u5730\u5740\u2192<\/p>\n<h2>\u6837\u4f8b 1:<\/h2>\n<pre><code>\u8f93\u5165:{1,2,3} \u8f93\u51fa:[[1],[3,2]] \u89e3\u91ca:     1    \/    2   3 \u5b83\u5c06\u88ab\u5e8f\u5217\u5316\u4e3a {1,2,3} <\/code><\/pre>\n<h2>\u6837\u4f8b 2:<\/h2>\n<pre><code>\u8f93\u5165:{3,9,20,#,#,15,7} \u8f93\u51fa:[[3],[20,9],[15,7]] \u89e3\u91ca:     3    \/    9  20     \/      15   7 \u5b83\u5c06\u88ab\u5e8f\u5217\u5316\u4e3a {3,9,20,#,#,15,7} <\/code><\/pre>\n<h1>[\u9898\u89e3]<\/h1>\n<p> <\/p>\n<h2>\u7b97\u6cd5\uff1a\u6811\u7684\u5c42\u6b21\u904d\u5386<\/h2>\n<p>\u5c42\u6b21\u904d\u5386\uff0c\u53ef\u4ee5\u8fd0\u7528\u5e7f\u5ea6\u904d\u5386\u7684\u601d\u60f3\u5b9e\u73b0\u4ece\u4e0a\u5f80\u4e0b\u7684\u9010\u5c42\u904d\u5386\u3002\u4ece\u5934\u7ed3\u70b9\u5f00\u59cb\u9010\u5c42\u904d\u5386\uff0c\u5f00\u8f9f\u4e00\u4e2a\u65b0\u961f\u5217\uff0c\u8ba9\u5934\u7ed3\u70b9\u5165\u961f\u5e76\u8ba1\u7b97\u6b64\u65f6\u7684\u957f\u5ea6\uff0c\u6bcf\u6b21\u90fd\u5c06\u5f53\u524d\u5c42\u7684\u5b50\u8282\u70b9\u5168\u90e8\u538b\u5165\u961f\u5217\uff0c\u7136\u540e\u5bf9\u4e0b\u4e00\u5c42\u7684\u8282\u70b9\u8fdb\u884c\u904d\u5386\uff0c\u518d\u5c06\u4e0b\u4e00\u5c42\u7684\u5b50\u8282\u70b9\u538b\u5165\u961f\u5217\uff0c\u4e0d\u65ad\u5faa\u73af\uff0c\u4e00\u76f4\u904d\u5386\u5230\u5e95\u5c42\uff0c\u5224\u65ad\u7684\u7ec8\u6b62\u6761\u4ef6\u5c31\u662f\u961f\u5217\u4e0d\u4e3a\u7a7a\u3002<\/p>\n<ul>\n<li>\u5faa\u73af\u91cc\u9762\uff0c\u961f\u5217\u5934\u51fa\u961f\uff0c\u5224\u65ad\u5176\u662f\u5426\u6709\u5de6\u53f3\u5b50\u7ed3\u70b9\uff0c\u5982\u679c\u6709\uff0c\u5219\u5c06\u6b64\u70b9\u7684\u5b50\u8282\u70b9\u5165\u961f\uff0c\u4f46\u6b64\u65f6\u8fd8\u4e0d\u9700\u8981\u66f4\u65b0\u961f\u5217\u7684\u957f\u5ea6\uff0c\u5f53\u524d\u961f\u5217\u7684\u957f\u5ea6\u662f\u6bcf\u5c42\u7684\u957f\u5ea6\u3002\u5f53\u8fd9\u5c42\u7684\u957f\u5ea6\u51cf\u4e3a 0 \u65f6\uff0c\u5c31\u8bf4\u660e\u8fd9\u5c42\u7684\u904d\u5386\u7ed3\u675f\uff0c\u5f00\u59cb\u66f4\u65b0\u957f\u5ea6\u4e3a\u4e0b\u4e00\u5c42\u7684\u957f\u5ea6\u3002<\/li>\n<li>\u51fa\u961f\u7684\u5143\u7d20\u7684\u503c\u6309\u7167\u4e00\u5c42\u5c42\u538b\u5165\u7ed3\u679c\u6570\u7ec4<\/li>\n<li>\u56e0\u4e3a\u9898\u76ee\u952f\u9f7f\u5f62\u904d\u5386<\/li>\n<li>\u6211\u4eec\u7528\u4e00\u4e2a isforward \u6807\u8bb0\u5f53\u524d\u65b9\u5411\uff0c\u6bcf\u904d\u5386\u5b8c\u4e00\u5c42\uff0c\u5982\u679c\u662f\u53cd\u5411\u7684\uff0c\u5219\u5c06\u8fd9\u5c42\u7684\u8282\u70b9\u6570\u7ec4\u5012\u5e8f\uff0c\u7136\u540e\u5c06\u8fd9\u5c42\u7684\u96c6\u5408\u538b\u5165\u7ed3\u679c<\/li>\n<\/ul>\n<h2>\u590d\u6742\u5ea6\u5206\u6790<\/h2>\n<h3>\u65f6\u95f4\u590d\u6742\u5ea6 O(n)<\/h3>\n<p>n \u4e3a\u8282\u70b9\u6570\u91cf<\/p>\n<h3>\u7a7a\u95f4\u590d\u6742\u5ea6 O(n)<\/h3>\n<p>\u5b58\u4e0b\u6240\u6709\u70b9\u7684\u4fe1\u606f n \u4e3a\u8282\u70b9\u6570\u91cf<\/p>\n<pre><code> public class Solution {     \/**      * @param root: A Tree      * @return: A list of lists of integer include the zigzag level order traversal of its nodes' values.      *\/     public List&lt;List&lt;Integer&gt;&gt; zigzagLevelOrder(TreeNode root){         List&lt;List&lt;Integer&gt;&gt; ans = new ArrayList&lt;List&lt;Integer&gt;&gt;();         if (root == null) {             return ans;         }         Queue&lt;TreeNode&gt; q = new LinkedList&lt;TreeNode&gt;();         \/\/\u6b63\u53cd\u5411\u6807\u5fd7         boolean isForward = true;         q.offer(root);         while (!q.isEmpty()) {             int size = q.size();             List&lt;Integer&gt; subList = new ArrayList&lt;Integer&gt;();             for (int i = 0 ; i &lt; size ; i++) {                 TreeNode treeNode = q.poll();                 subList.add(treeNode.val);                 if (treeNode.left != null) {                      q.offer(treeNode.left);                 }                 if (treeNode.right != null) {                     q.offer(treeNode.right);                 }             }             \/\/\u6839\u636e\u6807\u5fd7\u6765\u786e\u8ba4\u5f53\u524d\u5c42\u904d\u5386\u7684\u65b9\u5411             if (!isForward) {                 Collections.reverse(subList);\/\/\u7ffb\u8f6c             }             ans.add(subList);             \/\/\u65b9\u5411\u53cd\u8f6c             isForward = !isForward;         }         return ans;     } } <\/code><\/pre>\n<p>\u66f4\u591a\u9898\u89e3\u53c2\u89c1<\/p>\n<\/p><\/div>\n<div> <b>\u5927\u4f6c\u6709\u8a71\u8aaa<\/b> (<span>2<\/span>)        <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<ul>\n<li data-pid=\"2989488\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : vicsun2020 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u8fd9\u4e48\u5bb9\u6613\u7684\u561b                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"2989489\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : zzzrf <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @vicsun2020 hard \u9898\u4e5f\u4f1a\u6709\uff0c\u4e5f\u6709\u76f8\u5bf9\u5bb9\u6613\u7684\u9898                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li>\n","protected":false},"excerpt":{"rendered":"<p>\u5de8\u786c\u5bb6\u9762\u8bd5\u9898\uff1a\u4e8c\u53c9\u6811\u7684\u952f\u9f7f\u5f62\u5c42\u6b21\u904d&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\/150664"}],"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=150664"}],"version-history":[{"count":0,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/150664\/revisions"}],"wp:attachment":[{"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=150664"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=150664"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=150664"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}