<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>zigzag Archives - Huahua&#039;s Tech Road</title>
	<atom:link href="https://zxi.mytechroad.com/blog/tag/zigzag/feed/" rel="self" type="application/rss+xml" />
	<link>https://zxi.mytechroad.com/blog/tag/zigzag/</link>
	<description></description>
	<lastBuildDate>Wed, 21 Aug 2019 05:19:27 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.0.8</generator>

<image>
	<url>https://zxi.mytechroad.com/blog/wp-content/uploads/2017/09/cropped-photo-32x32.jpg</url>
	<title>zigzag Archives - Huahua&#039;s Tech Road</title>
	<link>https://zxi.mytechroad.com/blog/tag/zigzag/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>花花酱 LeetCode 103. Binary Tree Zigzag Level Order Traversal</title>
		<link>https://zxi.mytechroad.com/blog/tree/leetcode-103-binary-tree-zigzag-level-order-traversal/</link>
					<comments>https://zxi.mytechroad.com/blog/tree/leetcode-103-binary-tree-zigzag-level-order-traversal/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Wed, 21 Aug 2019 05:10:56 +0000</pubDate>
				<category><![CDATA[Tree]]></category>
		<category><![CDATA[level]]></category>
		<category><![CDATA[medium]]></category>
		<category><![CDATA[tree]]></category>
		<category><![CDATA[zigzag]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=5473</guid>

					<description><![CDATA[<p>Given a binary tree, return the&#160;zigzag level order&#160;traversal of its nodes&#8217; values. (ie, from left to right, then right to left for the next level&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/tree/leetcode-103-binary-tree-zigzag-level-order-traversal/">花花酱 LeetCode 103. Binary Tree Zigzag Level Order Traversal</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<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>



<p>For example:<br>Given binary tree&nbsp;<code>[3,9,20,null,null,15,7]</code>,<br></p>



<pre class="wp-block-preformatted;crayon:false">    3
   / \
  9  20
    /  \
   15   7
</pre>



<p>return its zigzag level order traversal as:<br></p>



<pre class="wp-block-preformatted;crayon:false">[
  [3],
  [20,9],
  [15,7]
]</pre>



<h2><strong>Solution 1: DFS</strong></h2>



<p>in order traversal using DFS and reverse the result of even levels.</p>



<p>Time complexity: O(n)<br>Space complexity: O(n)</p>



<div class="responsive-tabs">
<h2 class="tabtitle">C++</h2>
<div class="tabcontent">

<pre class="crayon-plain-tag">// Author: Huahua, 0ms, 15.1 MB
class Solution {
public:
  vector&lt;vector&lt;int&gt; &gt; zigzagLevelOrder(TreeNode *root) {
    vector&lt;vector&lt;int&gt;&gt; ans;
    function&lt;void(TreeNode*, int)&gt; dfs = [&amp;](TreeNode* r, int d) {
      if (!r) return;
      while (ans.size() &lt;= d) ans.push_back({});      
      ans[d].push_back(r-&gt;val);      
      dfs(r-&gt;right, d + 1);
      dfs(r-&gt;left, d + 1);
    };
    dfs(root, 0);    
    for (int i = 0; i &lt; ans.size(); i += 2)
      reverse(begin(ans[i]), end(ans[i]));      
    return ans;
  }
};</pre>
</div></div>



<h2><strong>Solution 2: BFS</strong></h2>



<p>Expend/append in order for even levels and doing that in reverse order for odd levels.</p>



<p>Time complexity: O(n)<br>Space complexity: O(n)</p>



<div class="responsive-tabs">
<h2 class="tabtitle">C++</h2>
<div class="tabcontent">

<pre class="crayon-plain-tag">// Author: Huahua
class Solution {
public:
  vector&lt;vector&lt;int&gt;&gt; zigzagLevelOrder(TreeNode *root) {
    if (!root) return {};
    vector&lt;vector&lt;int&gt;&gt; ans;
    deque&lt;TreeNode*&gt; q;
    q.push_back(root);
    int d = 0;
    while (q.size()) {
      ans.push_back({});    
      auto cur = &amp;ans.back();
      deque&lt;TreeNode*&gt; next;
      while (q.size()) {
        if (d % 2) {
          TreeNode* node = q.back();
          q.pop_back();
          cur-&gt;push_back(node-&gt;val);
          if (node-&gt;right) next.push_front(node-&gt;right);
          if (node-&gt;left) next.push_front(node-&gt;left);          
        } else {
          TreeNode* node = q.front();
          q.pop_front();
          cur-&gt;push_back(node-&gt;val);
          if (node-&gt;left) next.push_back(node-&gt;left);
          if (node-&gt;right) next.push_back(node-&gt;right);          
        }
      }
      ++d;
      q.swap(next);
    }
    return ans;
  }
};</pre>
</div></div>



<h2><strong>Related Problems</strong></h2>



<ul><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>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/tree/leetcode-103-binary-tree-zigzag-level-order-traversal/">花花酱 LeetCode 103. Binary Tree Zigzag Level Order Traversal</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://zxi.mytechroad.com/blog/tree/leetcode-103-binary-tree-zigzag-level-order-traversal/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>花花酱 LeetCode 6. ZigZag Conversion</title>
		<link>https://zxi.mytechroad.com/blog/simulation/leetcode-6-zigzag-conversion/</link>
					<comments>https://zxi.mytechroad.com/blog/simulation/leetcode-6-zigzag-conversion/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Wed, 12 Sep 2018 15:54:49 +0000</pubDate>
				<category><![CDATA[Simulation]]></category>
		<category><![CDATA[String]]></category>
		<category><![CDATA[conversion]]></category>
		<category><![CDATA[medium]]></category>
		<category><![CDATA[row]]></category>
		<category><![CDATA[string]]></category>
		<category><![CDATA[zigzag]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=3923</guid>

					<description><![CDATA[<p>Problem The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like this: (you may want to display this pattern in a&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/simulation/leetcode-6-zigzag-conversion/">花花酱 LeetCode 6. ZigZag Conversion</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></description>
										<content:encoded><![CDATA[<h1><strong>Problem</strong></h1>
<p>The string <code>"PAYPALISHIRING"</code> is written in a zigzag pattern on a given number of rows like this: (you may want to display this pattern in a fixed font for better legibility)</p>
<pre class="crayon:false">P   A   H   N
A P L S I I G
Y   I   R
</pre>
<p>And then read line by line: <code>"PAHNAPLSIIGYIR"</code></p>
<p>Write the code that will take a string and make this conversion given a number of rows:</p>
<pre class="crayon:false">string convert(string s, int numRows);</pre>
<p><strong>Example 1:</strong><br />
&lt;preclass=&#8221;crayon:false&#8221;&gt;<strong>Input:</strong> s = &#8220;PAYPALISHIRING&#8221;, numRows = 3 <strong>Output:</strong> &#8220;PAHNAPLSIIGYIR&#8221;</p>
<p><strong>Example 2:</strong></p>
<pre class="crayon:false"><strong>Input:</strong> s = "PAYPALISHIRING", numRows = 4
<strong>Output:</strong> "PINALSIGYAHRPI"
<strong>Explanation:</strong>

P     I    N
A   L S  I G
Y A   H R
P     I</pre>
<h1><strong>Solution: Simulation</strong></h1>
<p>Store the zigzag results for each row and then output row by row.</p>
<p>Time complexity: O(n)</p>
<p>Space complexity: O(n)</p>
<p><div class="responsive-tabs">
<h2 class="tabtitle">C++</h2>
<div class="tabcontent">
</p><pre class="crayon-plain-tag">// Author: Huahua
class Solution {
public:
  string convert(string s, int nRows) {
    if (nRows == 1) return s;

    vector&lt;string&gt; ss(nRows);
    int l = s.length();
    int x = 0, y = 0, i = 0;
    bool down = true;
    while (i &lt; l) {
      ss[y] += s[i];
      if (down) {
        ++y;
        if (y == nRows) {
            down = false;
            y -=2;
        }
      } else {
        --y;
        if (y &lt; 0) {
            down = true;
            y = 1;
        }
      }
      i++;
    }

    string ans;
    for(const string&amp; r : ss)
      ans += r;
    return ans;
  }
};</pre><p></div></div></p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/simulation/leetcode-6-zigzag-conversion/">花花酱 LeetCode 6. ZigZag Conversion</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://zxi.mytechroad.com/blog/simulation/leetcode-6-zigzag-conversion/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
