{"id":5473,"date":"2019-08-20T22:10:56","date_gmt":"2019-08-21T05:10:56","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=5473"},"modified":"2019-08-20T22:19:27","modified_gmt":"2019-08-21T05:19:27","slug":"leetcode-103-binary-tree-zigzag-level-order-traversal","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/tree\/leetcode-103-binary-tree-zigzag-level-order-traversal\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 103. Binary Tree Zigzag Level Order Traversal"},"content":{"rendered":"\n<p>Given a binary tree, return the&nbsp;<em>zigzag level order<\/em>&nbsp;traversal of its nodes&#8217; values. (ie, from left to right, then right to left for the next level and alternate between).<\/p>\n\n\n\n<p>For example:<br>Given binary tree&nbsp;<code>[3,9,20,null,null,15,7]<\/code>,<br><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\">    3\n   \/ \\\n  9  20\n    \/  \\\n   15   7\n<\/pre>\n\n\n\n<p>return its zigzag level order traversal as:<br><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\">[\n  [3],\n  [20,9],\n  [15,7]\n]<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution 1: DFS<\/strong><\/h2>\n\n\n\n<p>in order traversal using DFS and reverse the result of even levels.<\/p>\n\n\n\n<p>Time complexity: O(n)<br>Space complexity: O(n)<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"C++\">\n\/\/ Author: Huahua, 0ms, 15.1 MB\nclass Solution {\npublic:\n  vector<vector<int> > zigzagLevelOrder(TreeNode *root) {\n    vector<vector<int>> ans;\n    function<void(TreeNode*, int)> dfs = [&](TreeNode* r, int d) {\n      if (!r) return;\n      while (ans.size() <= d) ans.push_back({});      \n      ans[d].push_back(r->val);      \n      dfs(r->right, d + 1);\n      dfs(r->left, d + 1);\n    };\n    dfs(root, 0);    \n    for (int i = 0; i < ans.size(); i += 2)\n      reverse(begin(ans[i]), end(ans[i]));      \n    return ans;\n  }\n};\n<\/pre>\n<\/div><\/div>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution 2: BFS<\/strong><\/h2>\n\n\n\n<p>Expend\/append in order for even levels and doing that in reverse order for odd levels.<\/p>\n\n\n\n<p>Time complexity: O(n)<br>Space complexity: O(n)<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"C++\">\n\/\/ Author: Huahua\nclass Solution {\npublic:\n  vector<vector<int>> zigzagLevelOrder(TreeNode *root) {\n    if (!root) return {};\n    vector<vector<int>> ans;\n    deque<TreeNode*> q;\n    q.push_back(root);\n    int d = 0;\n    while (q.size()) {\n      ans.push_back({});    \n      auto cur = &ans.back();\n      deque<TreeNode*> next;\n      while (q.size()) {\n        if (d % 2) {\n          TreeNode* node = q.back();\n          q.pop_back();\n          cur->push_back(node->val);\n          if (node->right) next.push_front(node->right);\n          if (node->left) next.push_front(node->left);          \n        } else {\n          TreeNode* node = q.front();\n          q.pop_front();\n          cur->push_back(node->val);\n          if (node->left) next.push_back(node->left);\n          if (node->right) next.push_back(node->right);          \n        }\n      }\n      ++d;\n      q.swap(next);\n    }\n    return ans;\n  }\n};\n<\/pre>\n<\/div><\/div>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Related Problems<\/strong><\/h2>\n\n\n\n<ul class=\"wp-block-list\"><li><a href=\"https:\/\/zxi.mytechroad.com\/blog\/leetcode\/leetcode-102-binary-tree-level-order-traversal\/\">https:\/\/zxi.mytechroad.com\/blog\/leetcode\/leetcode-102-binary-tree-level-order-traversal\/<\/a><\/li><li><a href=\"https:\/\/zxi.mytechroad.com\/blog\/tree\/leetcode-637-average-of-levels-in-binary-tree\/\">https:\/\/zxi.mytechroad.com\/blog\/tree\/leetcode-637-average-of-levels-in-binary-tree\/<\/a><\/li><li><a href=\"https:\/\/zxi.mytechroad.com\/blog\/tree\/leetcode-429-n-ary-tree-level-order-traversal\/\">https:\/\/zxi.mytechroad.com\/blog\/tree\/leetcode-429-n-ary-tree-level-order-traversal\/<\/a><\/li><\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Given a binary tree, return the&nbsp;zigzag level order&nbsp;traversal of its nodes&#8217; values. (ie, from left to right, then right to left for the next level&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[45],"tags":[329,177,28,400],"class_list":["post-5473","post","type-post","status-publish","format-standard","hentry","category-tree","tag-level","tag-medium","tag-tree","tag-zigzag","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/5473","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/comments?post=5473"}],"version-history":[{"count":4,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/5473\/revisions"}],"predecessor-version":[{"id":5477,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/5473\/revisions\/5477"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=5473"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=5473"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=5473"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}