<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by arunachalamraj06 on Medium]]></title>
        <description><![CDATA[Stories by arunachalamraj06 on Medium]]></description>
        <link>https://medium.com/@arunachalamraj06?source=rss-4b7d7b8e1135------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*lwuYj3ilO-Y-ZlhHiHQgSA.jpeg</url>
            <title>Stories by arunachalamraj06 on Medium</title>
            <link>https://medium.com/@arunachalamraj06?source=rss-4b7d7b8e1135------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 06 Dec 2026 06:36:03 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@arunachalamraj06/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Construct Binary Tree from preorder and inorder traversal]]></title>
            <link>https://medium.com/@arunachalamraj06/construct-binary-tree-from-preorder-and-inorder-traversal-858113ab542b?source=rss-4b7d7b8e1135------2</link>
            <guid isPermaLink="false">https://medium.com/p/858113ab542b</guid>
            <category><![CDATA[construct-binary-tree]]></category>
            <category><![CDATA[tree-construction]]></category>
            <dc:creator><![CDATA[arunachalamraj06]]></dc:creator>
            <pubDate>Mon, 15 Sep 2025 17:46:53 GMT</pubDate>
            <atom:updated>2025-09-15T17:46:53.003Z</atom:updated>
            <content:encoded><![CDATA[<blockquote><strong>Problem statement:</strong></blockquote><p>Given two integer arrays preorder and inorder where preorder is the preorder traversal of a binary tree and inorder is the inorder traversal of the same tree, construct and return the binary tree.</p><p><strong>Input:</strong> preorder = [3,9,20,15,7], inorder = [9,3,15,20,7]</p><p><strong>Output:</strong> [3,9,20,null,null,15,7]</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/324/1*w97Ur7ZhIQrAvYWNOWNtXA.png" /><figcaption>input image</figcaption></figure><blockquote><strong>Solution:</strong></blockquote><p><strong>Intuition:</strong></p><p>From the problem statement, we need to <strong>reconstruct a binary tree</strong> using two traversal sequences: <strong>preorder</strong> and <strong>inorder</strong>.</p><p>Preorder Traversal: <strong>Root → Left → Right</strong></p><p>Inorder Traversal: <strong>Left → Root → Right</strong></p><p>The key idea is:</p><ul><li>The preorder array always gives us the root node first.</li><li>The inorder array tells us how to split the tree into left and right subtrees.</li></ul><p><strong>For example:</strong></p><ul><li>The first element in preorder is 3 → root of the tree.</li><li>In inorder, the elements to the left of 3 → [9] → left subtree.</li><li>The elements to the right of 3 → [15, 20, 7] → right subtree.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*OFrf0FWF9_oSSL9sqDM2ww.png" /><figcaption>iteration of forming binary tree</figcaption></figure><p><strong>Steps:</strong></p><p>1. Create a hashmap to quickly find the index of any element in the inorder array.</p><p>2. Use a recursive helper function to build the tree.</p><p>3. In each recursive call:</p><ul><li>Pick the current root from preorder.</li><li>Find its index in inorder to divide into left and right subtrees.</li><li>Recursively build the left and right subtrees.</li></ul><p>4. Return the root node of the constructed tree.</p><p><strong>Code:</strong></p><pre>from typing import List, Optional<br><br>class TreeNode:<br>    def __init__(self, val=0, left=None, right=None):<br>        self.val = val<br>        self.left = left<br>        self.right = right<br><br>class Solution:<br>    def buildTree(self, preorder: List[int], inorder: List[int]) -&gt; Optional[TreeNode]:<br>        <br>        self.indx_map = {inorder[i] : i for i in range(len(inorder))}<br>        self.index = 0<br>        root = self.helper(0, len(preorder)-1, preorder, inorder)<br>        return root<br>    <br>    def helper(self, start, end, preorder, inorder):<br><br>        if start &gt; end:<br>            return None<br>        <br>        root_val = preorder[self.index]<br>        self.index += 1<br>        root = TreeNode(root_val)<br><br>        mid = self.indx_map[root_val]<br><br>        root.left = self.helper(start, mid - 1, preorder, inorder)<br>        root.right = self.helper(mid + 1, end, preorder, inorder)<br><br>        return root</pre><p><strong>Analysis:</strong></p><p><strong>Time Complexity : </strong>O(N) where N is the number of nodes in the preorder array.</p><p><strong>Space Complexity: </strong>O(N) for the hashmap and recursion stack.</p><p><strong>Nandri…</strong></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=858113ab542b" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Maximum Width of Binary Tree [DSA — Tree Problem]]]></title>
            <link>https://medium.com/@arunachalamraj06/maximum-width-of-binary-tree-dsa-tree-problem-2b3368abff4b?source=rss-4b7d7b8e1135------2</link>
            <guid isPermaLink="false">https://medium.com/p/2b3368abff4b</guid>
            <category><![CDATA[max-width-of-binary-tree]]></category>
            <category><![CDATA[level-order-traversal]]></category>
            <dc:creator><![CDATA[arunachalamraj06]]></dc:creator>
            <pubDate>Sun, 14 Sep 2025 12:54:59 GMT</pubDate>
            <atom:updated>2025-09-14T12:54:59.521Z</atom:updated>
            <content:encoded><![CDATA[<h3>Maximum Width of Binary Tree [DSA — Tree Problem]</h3><blockquote><strong>Problem Statement:</strong></blockquote><p>Given the root of a binary tree, return the <strong>maximum width</strong> of the given tree.</p><p>The maximum width of a tree is the maximum width among all levels.</p><p>The width of one level is defined as the length between the end-nodes (the leftmost and rightmost non-null nodes), where the null nodes between the end-nodes that would be present in a complete binary tree extending down to that level are also counted into the length calculation.</p><p>It is guaranteed that the answer will in the range of a 32-bit signed integer.</p><p><strong>Input:</strong> root = [1,3,2,5,3,null,9]</p><p><strong>Output:</strong> 4</p><p><strong>Explanation:</strong> The maximum width exists in the third level with length 4 (5,3,null,9).</p><blockquote><strong>Solution:</strong></blockquote><p><strong>Intuition:</strong></p><p>By reading the problem statement carefully,</p><p>We want to find the maximum width of a binary tree.</p><p><strong>How to calculate the width at each level?</strong></p><p>Width of a level = <strong>rightmost index — leftmost index + 1.</strong></p><p>This automatically includes all the in-between positions, even the null nodes.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/683/1*c6GoNgWN5T29LXe5Gbw49Q.png" /><figcaption>finding the with for each level</figcaption></figure><p><strong>How do we assign indices to nodes?</strong></p><p>We use the idea of a complete binary tree representation (like heap indexing):</p><p>Assign the root node → index 0.</p><p>For any node at index i:</p><p><strong>Left child → 2 * i + 1</strong></p><p><strong>Right child → 2 * i + 2</strong></p><p>This way, every node gets a unique position index that represents its place in a complete binary tree</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/597/1*7kWZTxPUKdPa6GJWXHt_Qw.png" /><figcaption>assigning indexes to nodes</figcaption></figure><p><strong>Steps:</strong></p><p>1. Initialize a queue for level order traversal, starting with the root node and its index (0).</p><p>2. For each level, record the index of the first node (level_start).</p><p>3. For each node in the level, calculate its index based on its parent’s index.</p><p>4. After processing all nodes at the current level, calculate the width as (last_index — level_start + 1) and update max_width if this width is greater.</p><p>5. Continue until all levels are processed.</p><p>6. Return max_width.</p><p><strong>Code:</strong></p><pre>from collections import deque<br>from typing import Optional<br><br>class TreeNode:<br>    def __init__(self, val=0, left=None, right=None):<br>        self.val = val<br>        self.left = left<br>        self.right = right<br><br>class Solution:<br>    def widthOfBinaryTree(self, root: Optional[TreeNode]) -&gt; int:<br>        if not root:<br>            return 0<br>        <br>        max_width = 0<br>        queue = deque([(root, 0)])<br>        <br>        while queue:<br>            level_length = len(queue)<br>            _, level_start = queue[0]<br>            <br>            for _ in range(level_length):<br>                node, index = queue.popleft()<br>                if node.left:<br>                    queue.append((node.left, 2 * index))<br>                if node.right:<br>                    queue.append((node.right, 2 * index + 1))<br>            <br>            max_width = max(max_width, index - level_start + 1)<br>        <br>        return max_width</pre><p><strong>Analysis:</strong></p><p><strong>Time Complexity: O(N)</strong> where N is number of nodes in the tree.</p><p><strong>Space Complexity: O(N)</strong> for the queue.</p><p><strong>Nandri…</strong></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=2b3368abff4b" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Binary Tree Paths [DSA — Tree Problem]]]></title>
            <link>https://medium.com/@arunachalamraj06/binary-tree-paths-dsa-tree-problem-fdce5c3f96fb?source=rss-4b7d7b8e1135------2</link>
            <guid isPermaLink="false">https://medium.com/p/fdce5c3f96fb</guid>
            <category><![CDATA[breadth-first-search]]></category>
            <category><![CDATA[bfs-and-dfs]]></category>
            <category><![CDATA[binary-tree-paths]]></category>
            <dc:creator><![CDATA[arunachalamraj06]]></dc:creator>
            <pubDate>Thu, 11 Sep 2025 18:19:00 GMT</pubDate>
            <atom:updated>2025-09-11T18:19:00.415Z</atom:updated>
            <content:encoded><![CDATA[<h3>Binary Tree Paths [DSA — Tree Problem]</h3><blockquote><strong>Problem Statement:</strong></blockquote><p>Given the root of a binary tree, return all root-to-leaf paths in any order.</p><p>A leaf is a node with no children.</p><p><strong>Input:</strong> root = [1,2,3,null,5]</p><p><strong>Output: </strong>[“1-&gt;2-&gt;5”,”1-&gt;3&quot;]</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/432/1*7mlimZqZ2CWW-Fsd135hag.png" /><figcaption>Input</figcaption></figure><blockquote><strong>Solution:</strong></blockquote><p><strong>Intuition:</strong></p><p>The problem asks us to return all paths from the root to every leaf node in a binary tree.</p><p>This means as we traverse the tree, we need to keep building the path string from the root down to the current node.</p><p>When we finally reach a leaf (a node with no left and right children), the path we’ve built so far is a complete root-to-leaf path, and we add it to the result list.</p><p>We can use either DFS or BFS for this purpose. Here, we will implement the BFS approach using a queue.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/659/1*JOHDRycZl7kpHPo4yax77w.png" /><figcaption>bfs steps image</figcaption></figure><p><strong>Steps:</strong></p><p>1. Initialize an empty list to store the result paths and a queue to facilitate BFS.</p><p>2. Start by adding the root node and an empty string to the queue.</p><p>3. While the queue is not empty, pop the front element which contains the current node and the path string.</p><p>4. If the current node is a leaf node, append the path string to the result list.</p><p>5. If the current node has a left child, add it to the queue with the updated path string.</p><p>6. If the current node has a right child, add it to the queue with the updated path string.</p><p>7. Continue this process until the queue is empty.</p><p>8. Return the result list containing all root-to-leaf paths.</p><p><strong>Code:</strong></p><pre>from collections import deque<br><br>class TreeNode:<br>    def __init__(self, val=0, left=None, right=None):<br>        self.val = val<br>        self.left = left<br>        self.right = right<br><br>class Solution:<br>    def binaryTreePaths(self, root: Optional[TreeNode]) -&gt; List[str]:<br>        if not root:<br>            return []<br>        <br>        res, queue = [], deque()<br>        queue.append((root, &quot;&quot;))<br><br>        while queue:<br>            node, path = queue.popleft()<br><br>            if not node.left and not node.right:<br>                res.append(path + str(node.val))<br><br>            if node.left:<br>                queue.append((node.left, path + str(node.val) + &quot;-&gt;&quot;))<br>            if node.right:<br>                queue.append((node.right, path + str(node.val) + &quot;-&gt;&quot;))<br>        <br>        return res</pre><p><strong>Analysis:</strong></p><p><strong>Time Complexity: O(N)</strong> where N is the number of nodes in the binary tree.</p><p><strong>Space Complexity: O(N)</strong> for the queue and the result list.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=fdce5c3f96fb" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Maximum Depth of Binary Tree [DSA — Tree Problem]]]></title>
            <link>https://medium.com/@arunachalamraj06/maximum-depth-of-binary-tree-dsa-tree-problem-6a09f127cae9?source=rss-4b7d7b8e1135------2</link>
            <guid isPermaLink="false">https://medium.com/p/6a09f127cae9</guid>
            <category><![CDATA[breadth-first-search]]></category>
            <category><![CDATA[depth-first-search]]></category>
            <category><![CDATA[max-depth-binary-tree]]></category>
            <dc:creator><![CDATA[arunachalamraj06]]></dc:creator>
            <pubDate>Tue, 09 Sep 2025 19:28:45 GMT</pubDate>
            <atom:updated>2025-09-09T19:28:45.221Z</atom:updated>
            <content:encoded><![CDATA[<h3>Maximum Depth of Binary Tree [DSA — Tree Problem]</h3><blockquote><strong>Problem Statement:</strong></blockquote><p>Given the root of a binary tree, return its maximum depth.</p><p>A binary tree’s maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node.</p><p><strong>Input:</strong> root = [3,9,20,null,null,15,7]</p><p><strong>Output: </strong>3</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/351/1*h3Qgh0sqzfi9HQmYrRbe3w.png" /><figcaption>input image</figcaption></figure><blockquote><strong>Solution:</strong></blockquote><p>To find the maximum depth of the binary tree, we have two ways, which are <strong>depth first search</strong> and <strong>breadth first search.</strong></p><p>We will look both the ways and understand how depth first search and breadth first search works.</p><p><strong>Intuition 1: Depth First Search (DFS)</strong></p><p>Depth-first search (DFS) is a recursive algorithm used to explore all nodes of a binary tree. To find the maximum depth, we traverse the tree while keeping track of the depth at each node.</p><p>Let’s consider a small subtree with the nodes 20, 15, and 7. Here, node 20 has two children: 15 and 7. If we look at the children, both 15 and 7 are leaf nodes, which means their maximum depth is 1.</p><p>However, when we calculate the depth of the entire subtree rooted at 20, we cannot stop at just the children. we must also include the current node itself.</p><p>That’s why the depth of this subtree becomes 1 (for node 20) plus the maximum depth of its children.</p><p>Mathematically, this is written as 1 + max(1, 1) = 2.</p><p>So, the maximum depth of the subtree rooted at 20 is 2.</p><p>This simple example highlights the recursive nature of the problem: at each node, the depth is always 1 (for the current node) plus the greater depth of its left and right subtrees.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/685/1*UXPOtTqPrSxRhHY8ybZygw.png" /><figcaption>depth first search recursive</figcaption></figure><p><strong>Steps:</strong></p><p>1. If the current node is null, return 0 (base case).</p><p>2. Recursively calculate the maximum depth of the left and right subtrees.</p><p>3. The maximum depth of the current node is 1 (for the current node) plus the maximum of the depths of the left and right subtrees.</p><p>4. Return the calculated maximum depth.</p><p><strong>Code:</strong></p><pre>class Solution:<br>    def maxDepth(self, root: Optional[TreeNode]) -&gt; int:<br>        if not root:<br>            return 0<br>        <br>        return 1 + max(self.maxDepth(root.left), self.maxDepth(root.right))</pre><p><strong>Analysis:</strong></p><p><strong>Time Complexity :</strong> O(n) where n is the number of nodes in the binary tree.</p><p><strong>Space Complexity :</strong> O(h) where h is the height of the binary tree.</p><p><strong>Intuition 2: Breadth First Search (BFS)</strong></p><p>Breadth-first search (BFS) is an iterative algorithm that explores the binary tree level by level. The maximum depth is simply the number of levels traversed.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/717/1*60JbtOFlslBuvjC_q0JZgA.png" /></figure><p><strong>Steps:</strong></p><p>1. If the root is null, return 0.</p><p>2. Initialize a queue and add the root node to it.</p><p>3. Initialize a variable depth to 0.</p><p>4. While the queue is not empty:</p><ul><li>a. Increment the depth by 1.</li><li>b. For each node in the current level (i.e., the number of nodes in the queue):</li><li>i. Dequeue a node from the front of the queue.</li><li>ii. If the node has a left child, enqueue it.</li><li>iii. If the node has a right child, enqueue it.</li></ul><p>5. Return the depth.</p><p><strong>Code:</strong></p><pre>class Solution:<br>    def maxDepth(self, root: Optional[TreeNode]) -&gt; int:<br>        if not root:<br>            return 0<br>        <br>        q = deque()<br>        q.append(root)<br>        depth = 0<br>        <br>        while q:<br>            depth += 1<br>            <br>            for _ in range(len(q)):<br>                node = q.popleft()<br>                if node.left:<br>                    q.append(node.left)<br>                if node.right:<br>                    q.append(node.right)<br>        <br>        return depth        </pre><p><strong>Analysis:</strong></p><p><strong>Time Complexity : </strong>O(n) where n is the number of nodes in the binary tree.</p><p><strong>Space Complexity : </strong>O(w) where w is the maximum width of the binary tree.</p><p><strong>Nandri…</strong></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=6a09f127cae9" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Add one row to tree [DSA — Tree Problem]]]></title>
            <link>https://medium.com/@arunachalamraj06/add-one-row-to-tree-dsa-tree-problem-38e8519cd01c?source=rss-4b7d7b8e1135------2</link>
            <guid isPermaLink="false">https://medium.com/p/38e8519cd01c</guid>
            <category><![CDATA[level-order-traversal]]></category>
            <category><![CDATA[add-row-to-bt]]></category>
            <dc:creator><![CDATA[arunachalamraj06]]></dc:creator>
            <pubDate>Sun, 07 Sep 2025 19:42:58 GMT</pubDate>
            <atom:updated>2025-09-07T19:42:58.468Z</atom:updated>
            <content:encoded><![CDATA[<h3>Add one row to tree [DSA — Tree Problem]</h3><blockquote><strong>Problem statement:</strong></blockquote><p>Given the <strong>root</strong> of a <strong>binary tree</strong> and two integers <strong>val</strong> and <strong>depth</strong>, add a row of nodes with value <strong>val</strong> at the given depth <strong>depth</strong>.</p><p>Note that the root node is at depth 1.</p><p>The adding rule is:</p><ul><li>Given the integer depth, for each not null tree node cur at the depth depth — 1, create two tree nodes with value val as cur’s left subtree root and right subtree root.</li><li>cur’s original left subtree should be the left subtree of the new left subtree root.</li><li>cur’s original right subtree should be the right subtree of the new right subtree root.</li><li>If depth == 1 that means there is no depth depth — 1 at all, then create a tree node with value val as the new root of the whole original tree, and the original tree is the new root’s left subtree.</li></ul><p><strong>Input: root</strong> = [4,2,6,3,1,5], <strong>val</strong> = 1, <strong>depth</strong> = 2</p><p><strong>Output:</strong> [4,1,1,2,null,null,6,3,1,5]</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1000/1*BdurZsuD8yYtj0IyrnB4tw.png" /><figcaption>input image</figcaption></figure><blockquote><strong>Solution:</strong></blockquote><p><strong>Intuition:</strong></p><p>On Reading the problem statement, First thing comes in my mind was BFS.</p><p><strong>Why BFS ?</strong> Since we need to add new nodes at a specific level, BFS is the natural choice. BFS processes nodes level by level, which makes it easier to stop exactly at the level just before the target depth.</p><p>We use a variable (say current_depth) to keep track of the current depth while traversing.</p><p>The root node is considered at depth = 1. So, we need to stop at depth — 1 (the parent level of where new nodes will be inserted).</p><p><strong>Special Case: depth = 1:</strong></p><ul><li>If the required depth is 1, we simply create a new root node with value val and attach the existing tree as its left child.</li></ul><p><strong>Inserting new nodes:</strong></p><ul><li>Once we reach depth — 1, we create new nodes:</li><li>A new left node that takes over the current node’s old left child.</li><li>A new right node that takes over the current node’s old right child.</li></ul><p>We first connect the old children to the new nodes before attaching the new nodes to the current node. Otherwise, we would lose the reference to the original subtrees.</p><p>After inserting the new row, our work is done.We don’t need to push anything else into the BFS queue.</p><p><strong>Steps:</strong></p><p>1. Initialize a queue with the root node and set current_depth = 1.</p><p>2. If depth == 1, create a new root and return.</p><p>3. Perform <strong>Breath First Search</strong> until current_depth == depth — 1.</p><p>4. For each node at this level:</p><ul><li>4.1. Create two new nodes with value val.</li><li>4.2. Reconnect old children under the new nodes.</li></ul><p>5. Return the original root.</p><p><strong>Code:</strong></p><pre>from collections import deque<br># class TreeNode:<br>#     def __init__(self, val=0, left=None, right=None):<br>#         self.val = val<br>#         self.left = left<br>#         self.right = right<br>class Solution:<br>    def addOneRow(self, root: Optional[TreeNode], val: int, depth: int) -&gt; Optional[TreeNode]:<br>        <br>        if depth == 1:  # case: add new root<br>            return TreeNode(val=val, left=root)<br><br>        q = deque([root])<br>        curr_depth = 1<br><br>        # Traverse until level before depth<br>        while curr_depth &lt; depth - 1:<br>            n = len(q)<br>            for _ in range(n):<br>                node = q.popleft()<br>                if node.left:<br>                    q.append(node.left)<br>                if node.right:<br>                    q.append(node.right)<br>            curr_depth += 1<br><br>        # Insert new row<br>        for _ in range(len(q)):<br>            node = q.popleft()<br>            node.left = TreeNode(val=val, left=node.left)<br>            node.right = TreeNode(val=val, right=node.right)<br>        <br>        return root</pre><p><strong>Analysis:</strong></p><p><strong>Time Complexity : O(N)</strong> where N = number of nodes in the tree.</p><p><strong>Space Complexity : O(W)</strong> where w is maximum width of the tree.</p><p>Nandri…</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=38e8519cd01c" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Reverse Odd Levels of Binary Tree [DSA — Tree Problem]]]></title>
            <link>https://medium.com/@arunachalamraj06/reverse-odd-levels-of-binary-tree-dsa-tree-problem-4c5167f5ccf0?source=rss-4b7d7b8e1135------2</link>
            <guid isPermaLink="false">https://medium.com/p/4c5167f5ccf0</guid>
            <category><![CDATA[level-order-traversal]]></category>
            <category><![CDATA[reverse-odd-level-bt]]></category>
            <dc:creator><![CDATA[arunachalamraj06]]></dc:creator>
            <pubDate>Sat, 06 Sep 2025 17:31:23 GMT</pubDate>
            <atom:updated>2025-09-06T17:31:23.757Z</atom:updated>
            <content:encoded><![CDATA[<h3><a href="https://leetcode.com/problems/reverse-odd-levels-of-binary-tree/">Reverse Odd Levels of Binary Tree</a> [DSA — Tree Problem]</h3><blockquote><strong>Problem Statement:</strong></blockquote><p>Given the root of a perfect binary tree, reverse the node values at each odd level of the tree.</p><ul><li>For example, suppose the node values at level 3 are [2,1,3,4,7,11,29,18], then it should become [18,29,11,7,4,3,1,2].</li></ul><p>Return the root of the reversed tree.</p><p>A binary tree is perfect if all parent nodes have two children and all leaves are on the same level.</p><p>The level of a node is the number of edges along the path between it and the root node.</p><p><strong>Input: root</strong> = [2,3,5,8,13,21,34]</p><p><strong>Output</strong>: [2,5,3,8,13,21,34]</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1007/1*N980Q7uBfq4DkRnEqWMw6w.png" /><figcaption>Input ouput explanation</figcaption></figure><p><strong>Explanation:</strong></p><p>The tree has only one odd level.</p><p>The nodes at level 1 are 3, 5 respectively, which are reversed and become 5, 3.</p><blockquote><strong>Solution:</strong></blockquote><h3><strong>Brute Force Approach</strong></h3><p><strong>Intuition:</strong></p><p>When first reading this problem, the straightforward idea is to perform a <strong>level-order traversal (BFS)</strong> of the binary tree. During the traversal, we can keep track of the levels and reverse the values at each odd level.</p><p>The easiest way to implement this is by:</p><ol><li>Storing the node values of the current level in a list.</li><li>If the level is odd, reverse this list.</li><li>Reassign the reversed values back to the same nodes.</li></ol><p>This works because BFS naturally gives us level-by-level processing, and reversing a list of values is simple. However, this requires <strong>extra space</strong> to store values separately before reassigning them.</p><p><strong>Steps:</strong></p><ol><li>Initialize a queue for level order traversal.</li><li>Traverse the tree level by level.</li><li>If the current level is <strong>odd</strong>, then:<br> 3.1 Collect the values of nodes at this level.<br> 3.2 Reverse the collected values.<br> 3.3 Reassign the reversed values back to the nodes.</li><li>Add child nodes to the queue for the next level.</li><li>Increment the level counter.</li><li>Continue until all levels are processed.</li><li>Return the modified tree.</li></ol><p><strong>Code:</strong></p><pre>from collections import deque<br><br>class Solution:<br>    def reverseOddLevels(self, root: Optional[TreeNode]) -&gt; Optional[TreeNode]:<br>        if not root:<br>            return None<br>        <br>        q = deque([root])<br>        level = 0<br><br>        while q:<br>            size = len(q)<br>            nodes = []<br><br>            # collect nodes at this level<br>            for _ in range(size):<br>                node = q.popleft()<br>                nodes.append(node)<br>                if node.left:<br>                    q.append(node.left)<br>                if node.right:<br>                    q.append(node.right)<br><br>            # reverse values if odd level<br>            if level % 2 == 1:<br>                values = [n.val for n in nodes]<br>                values.reverse() <br>                for i in range(len(nodes)):<br>                    nodes[i].val = values[i]<br><br>            level += 1<br>        <br>        return root</pre><p><strong>Analysis:</strong></p><p><strong>Time Complexity: </strong>O(N + reversing nodes at odd levels) where N is the number of nodes in the tree.<br><strong>Space Complexity: </strong>O(W + W) where W is the maximum width of the tree (maximum number of nodes at any level) and for reversing the width of the tree.</p><p><strong>Can we optimize this approach ? Yes we can.</strong></p><h3>Optimized Approach:</h3><p><strong>Intuition:</strong></p><p>If we look closely, we don’t actually need to store the node values in a list and then reverse them. Instead, we can directly swap the values of the nodes at each odd level.</p><p><strong>How do we do that?</strong> We take the first node and the last node of that level, swap their values, then move to the next pair (second node with second last), and continue this process until they meet in the middle.</p><p>This simple idea is known as the <strong>two-pointer approach</strong>, and it lets us reverse the nodes in place without needing extra space for storing values.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*eIcwMbMIGKECCmFiVfl4Vg.png" /><figcaption>Level order Traversal Iterations</figcaption></figure><p><strong>Steps:</strong></p><p>1. Initialize a queue for level order traversal (BFS).</p><p>2. Traverse the tree level by level.</p><p>3. If the current level is odd:</p><ul><li>3.1 Reverse the values of nodes at the current odd level in place using two-pointer technique.</li></ul><p>4. Add child nodes to the queue for the next level.</p><p>5. Increment the level counter.</p><p>6. Continue until all levels are processed.</p><p>7. Return the modified tree.</p><p><strong>Code:</strong></p><pre>from collections import deque<br>class TreeNode:<br>    def __init__(self, val=0, left=None, right=None):<br>        self.val = val<br>        self.left = left<br>        self.right = right<br><br>class Solution:<br>    def reverseOddLevels(self, root: Optional[TreeNode]) -&gt; Optional[TreeNode]:<br>        <br>        q = deque()<br>        q.append(root)<br>        level = 0<br>        res = []<br>        while len(q) &gt; 0:<br>            n = len(q)<br>            is_even = True if level % 2 == 0 else False<br>            if not is_even:<br>                l = 0<br>                r = n - 1<br>                while l &lt; r:<br>                    q[l].val, q[r].val = q[r].val, q[l].val<br>                    l += 1<br>                    r -= 1<br>                <br>            for i in range(n):<br>                ele = q.popleft()<br>                if ele.left is not None:<br>                    q.append(ele.left)<br>                if ele.right is not None:<br>                    q.append(ele.right)<br>            <br>            level += 1<br>        return root</pre><p><strong>Analysis:</strong></p><p><strong>Time Complexity:</strong> O(N) where N is the number of nodes in the tree.</p><p><strong>Space Complexity:</strong> O(W) where W is the maximum width of the tree (maximum number of nodes at any level).</p><p>Nandri…</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=4c5167f5ccf0" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Even Odd Tree [DSA — Tree Problem]]]></title>
            <link>https://medium.com/@arunachalamraj06/even-odd-tree-dsa-tree-problem-c8b775b0e2d5?source=rss-4b7d7b8e1135------2</link>
            <guid isPermaLink="false">https://medium.com/p/c8b775b0e2d5</guid>
            <category><![CDATA[level-order-traversal]]></category>
            <category><![CDATA[even-odd-tree-problem]]></category>
            <dc:creator><![CDATA[arunachalamraj06]]></dc:creator>
            <pubDate>Fri, 05 Sep 2025 20:57:40 GMT</pubDate>
            <atom:updated>2025-09-05T20:57:40.531Z</atom:updated>
            <content:encoded><![CDATA[<h3><a href="https://leetcode.com/problems/even-odd-tree/">Even Odd Tree</a> [DSA — Tree Problem]</h3><blockquote>Problem statement:</blockquote><p>A binary tree is named Even-Odd if it meets the following conditions:</p><ol><li>The root of the binary tree is at level index 0, its children are at level index 1, their children are at level index 2, etc.</li><li>For every even-indexed level, all nodes at the level have odd integer values in strictly increasing order (from left to right).</li><li>For every odd-indexed level, all nodes at the level have even integer values in strictly decreasing order (from left to right).</li><li>Given the root of a binary tree, return true if the binary tree is Even-Odd, otherwise return false.</li></ol><p><strong>Input: root</strong> = [1,10,4,3,null,7,9,12,8,6,null,null,2]<br><strong>Output</strong>: true<br><strong>Explanation: </strong>The node values on each level are:<br><strong>Level 0</strong>: [1]<br><strong>Level 1</strong>: [10,4]<br><strong>Level 2</strong>: [3,7,9]<br><strong>Level 3</strong>: [12,8,6,2]<br>Since levels 0 and 2 are all odd and increasing and levels 1 and 3 are all even and decreasing, the tree is Even-Odd.</p><blockquote>Solution:</blockquote><p><strong>Intuition:</strong></p><p>On Last Few Problems, We learned how to traverse the tree in level order. it is also same level order traversal kind of problem with some extra condition.</p><p>Identify how to statisfy those condition is the main thing.</p><p>On Even level Order, we have to check the node element is odd and should be increasing.</p><p>On Odd level Order, we have to check the node element is even and should be decreasing.</p><p>We have to find on which level we are in using counter.</p><p>Based on the order level we have to find whether we are even level order or odd level order.</p><p>Based on the Even / odd order level, we can iterate the element and check the node element is odd or even and increasing / decreasing respectively.</p><p><strong>Steps:</strong></p><ol><li>Intialize the queue and insert the root node.</li><li>Iterate each level until the queue is empty.</li><li>Find the number of nodes exist in queue and iterate all the nodes</li><li>On iterating the queue, pop the node element one by one.</li><li>While poping</li><li><strong>For even level order</strong>, check the node element is even and it is greater than past value, if it then return <strong>false</strong>.</li><li><strong>For odd level order</strong>, check the node element is odd and it is lesser than past value, if it then return <strong>false</strong>.</li><li>if the node not met the above condition, then insert the left child element and right child element.</li><li>Repeat the process until the queue is empty.</li><li>Return True.</li></ol><p><strong>Code:</strong></p><pre>from collections import deque<br>class TreeNode:<br>    def __init__(self, val=0, left=None, right=None):<br>        self.val = val<br>        self.left = left<br>        self.right = right<br><br>class Solution:<br>    def isEvenOddTree(self, root: Optional[TreeNode]) -&gt; bool:<br>        <br>        q = deque()<br>        q.append(root)<br>        counter = 0<br>        while len(q) &gt; 0:<br>            n = len(q)<br>            is_even = True if counter % 2 == 0 else False<br>            if is_even:<br>                past_element = float(&#39;-inf&#39;) <br>                for i in range(n):<br>                    ele = q.popleft()<br>                    if ele.val % 2 == 0:<br>                        return False<br>                    if ele.val &lt;= past_element:<br>                        return False<br>                    past_element = ele.val<br>                    if ele.left is not None:<br>                        q.append(ele.left)<br>                    if ele.right is not None:<br>                        q.append(ele.right)<br>            else:<br>                past_element = float(&#39;inf&#39;) <br>                for i in range(n):<br>                    ele = q.popleft()<br>                    if ele.val % 2 == 1:<br>                        return False<br>                    if ele.val &gt;= past_element:<br>                        return False<br>                    past_element = ele.val<br>                    if ele.left is not None:<br>                        q.append(ele.left)<br>                    if ele.right is not None:<br>                        q.append(ele.right)<br>       <br>            counter += 1<br>        <br>        return True</pre><p><strong>Analysis:</strong></p><p><strong>Tima Complexity : O(N)</strong> where n is the number of node values<br><strong>Space Complexity : O(N) </strong>where n is the number of node values</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=c8b775b0e2d5" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[ZigZag Level Order Traversal [DSA — Tree Problem]]]></title>
            <link>https://medium.com/@arunachalamraj06/zigzag-level-order-traversal-dsa-tree-problem-7dc102dd3d38?source=rss-4b7d7b8e1135------2</link>
            <guid isPermaLink="false">https://medium.com/p/7dc102dd3d38</guid>
            <category><![CDATA[zigzag-traversal]]></category>
            <category><![CDATA[binary-tree-traversal]]></category>
            <category><![CDATA[tree-traversal]]></category>
            <dc:creator><![CDATA[arunachalamraj06]]></dc:creator>
            <pubDate>Thu, 04 Sep 2025 18:04:24 GMT</pubDate>
            <atom:updated>2025-09-04T18:05:09.346Z</atom:updated>
            <content:encoded><![CDATA[<h3>ZigZag Level Order Traversal [DSA — Tree Problem]</h3><blockquote><strong>Problem Statement:</strong></blockquote><p>Given the root of a binary tree, return the zigzag level order traversal of its nodes’ values. (i.e., from left to right, then right to left for the next level and alternate between).</p><p><strong>Input: root</strong> = [3,9,20,null,null,15,7]</p><p><strong>Output:</strong> [[3],[9,20],[15,7]]</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/393/1*pTsP3BXyPhWQ2LSed0IYqQ.jpeg" /><figcaption>Input</figcaption></figure><blockquote><strong><em>Solution:</em></strong></blockquote><p><strong>Intuition:</strong></p><p>To solve the Zigzag Level Order Traversal problem, we first ask ourselves:</p><p><strong>What is the natural way to process a tree level by level?</strong></p><p>The answer is breadth-first search (BFS), because BFS explores one level at a time using a queue.</p><p>But then comes the twist: <strong>How do we introduce the zigzag pattern?</strong></p><p>Normally, BFS processes nodes from left to right at every level.</p><p>However, in this problem, we want to alternate directions left to right for one level, then right to left for the next.</p><p>So the next question is: <strong>How can we alternate directions without breaking the BFS process?</strong> One idea is to use a flag or counter. If the current level is even, we keep the order as it is. If the level is odd, we reverse the collected values before adding them to the result.</p><p>This way, the BFS traversal remains intact, but by simply checking the level number (even or odd), we achieve the zigzag effect.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*pMt-tRcKoKXlWQwKn3baSg.jpeg" /><figcaption>level order traversal explanation</figcaption></figure><p><strong>Steps:</strong></p><p>1. Initialize a queue to store nodes for each level, a result list to store the final traversal, and a counter to track the level number.</p><p>2. Add the root node to the queue.</p><p>3. While the queue is not empty:</p><ul><li>a. Get the number of nodes in the current level.</li><li>b. Create an empty list to store the current level’s values.</li><li>c. For each node in this level:</li><li>i. Remove the node from the queue.</li><li>ii. Add the node’s value to the current level’s list.</li><li>iii. If the node has a left child, add it to the queue.</li><li>iv. If the node has a right child, add it to the queue.</li><li>d. If the level counter is odd, reverse the current level’s list.</li><li>e. Add the current level’s list to the result.</li><li>f. Increment the level counter.</li></ul><p>4. Return the result list.</p><p><strong>Code:</strong></p><pre>from collections import deque<br>class TreeNode:<br>    def __init__(self, val=0, left=None, right=None):<br>        self.val = val<br>        self.left = left<br>        self.right = right<br><br>class Solution:<br>    def zigzagLevelOrder(self, root: Optional[TreeNode]) -&gt; List[List[int]]:<br>        <br>        q = deque()<br>        if root is None:<br>            return []<br>        q.append(root)<br>        res = []<br>        counter = 0<br>        while len(q) &gt; 0:<br>            n = len(q)<br>            ans = []<br>            for i in range(n):<br>                ele = q.popleft()<br>                if ele.left is not None:<br>                    q.append(ele.left)<br>                if ele.right is not None:<br>                    q.append(ele.right)<br>                ans.append(ele.val)<br>            if counter % 2 == 1:<br>                res.append(ans[::-1])<br>            else:<br>                res.append(ans)<br>            counter += 1<br>        <br>        return res</pre><p><strong>Analysis:</strong></p><p><strong>Time Complexity: O(N) </strong>where N is the number of nodes in the binary tree.</p><p><strong>Space Complexity: O(N)</strong> for the queue used in BFS traversal.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=7dc102dd3d38" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Level Order Traversal [DSA — Tree Problem]]]></title>
            <link>https://medium.com/@arunachalamraj06/level-order-traversal-dsa-tree-problem-cb5df4aed9b1?source=rss-4b7d7b8e1135------2</link>
            <guid isPermaLink="false">https://medium.com/p/cb5df4aed9b1</guid>
            <category><![CDATA[trees]]></category>
            <category><![CDATA[level-order-traversal]]></category>
            <category><![CDATA[binary-tree-traversal]]></category>
            <dc:creator><![CDATA[arunachalamraj06]]></dc:creator>
            <pubDate>Wed, 03 Sep 2025 16:50:36 GMT</pubDate>
            <atom:updated>2025-09-03T16:50:36.698Z</atom:updated>
            <content:encoded><![CDATA[<h3>Level Order Traversal [DSA — Tree Problem]</h3><blockquote><strong>Problem Statement:</strong></blockquote><p>Given the root of a binary tree, return the level order traversal of its nodes’ values. (i.e., from left to right, level by level).</p><p>Input: root = [3,9,20,null,null,15,7]</p><p>Output: [[3],[9,20],[15,7]]</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/393/1*pTsP3BXyPhWQ2LSed0IYqQ.jpeg" /></figure><blockquote><strong>Solution:</strong></blockquote><p><strong>Intuition:</strong></p><p>We can use a breadth-first search (BFS) approach to traverse the tree level by level. By using a queue, we can keep track of all the nodes at the current level and their children for the next level.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*X49dpiEdbKxz7Hv9wnlwmQ.jpeg" /></figure><p><strong>Steps:</strong></p><p>1. Use a queue to facilitate level order traversal (BFS).</p><p>2. Initialize the queue with the root node.</p><p>3. While the queue is not empty, process nodes level by level.</p><p>4. For each level, determine the number of nodes (n) at that level.</p><p>5. For each node at the current level, dequeue it, record its value, and enqueue its children (if they exist).</p><p>6. After processing all nodes at the current level, append the recorded values to the result list.</p><p>7. Return the result list after processing all levels.</p><p><strong>Code:</strong></p><pre>from collections import deque<br>class TreeNode:<br>    def __init__(self, val=0, left=None, right=None):<br>        self.val = val<br>        self.left = left<br>        self.right = right<br>        <br>class Solution:<br>    def levelOrder(self, root: Optional[TreeNode]) -&gt; List[List[int]]:<br>        <br>        q = deque()<br>        if root is None:<br>            return []<br>        q.append(root)<br>        res = []<br>        while len(q) &gt; 0:<br>            n = len(q)<br>            ans = []<br>            for i in range(n):<br>                ele = q.popleft()<br>                if ele.left is not None:<br>                    q.append(ele.left)<br>                if ele.right is not None:<br>                    q.append(ele.right)<br>                ans.append(ele.val)<br>            res.append(ans)<br>        <br>        return res</pre><p><strong>Analysis:</strong></p><p><strong>Time Complexity: O(N) </strong>where N is the number of nodes in the binary tree.</p><p><strong>Space Complexity: O(N)</strong> for the queue used in BFS traversal.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=cb5df4aed9b1" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Single element in a sorted Array [DSA — Array Problem]]]></title>
            <link>https://medium.com/@arunachalamraj06/single-element-in-a-sorted-array-dsa-array-problem-231d0704e5e8?source=rss-4b7d7b8e1135------2</link>
            <guid isPermaLink="false">https://medium.com/p/231d0704e5e8</guid>
            <category><![CDATA[binary-search-algorithm]]></category>
            <category><![CDATA[single-ele-in-sort-array]]></category>
            <dc:creator><![CDATA[arunachalamraj06]]></dc:creator>
            <pubDate>Tue, 02 Sep 2025 18:16:56 GMT</pubDate>
            <atom:updated>2025-09-02T18:16:56.286Z</atom:updated>
            <content:encoded><![CDATA[<h3>Single element in a sorted Array [DSA — Array Problem]</h3><blockquote><strong>Problem statement:</strong></blockquote><p>You are given a sorted array consisting of only integers where every element appears exactly twice, except for one element which appears exactly once.</p><p>Return the single element that appears only once.</p><p>Your solution must run in O(log n) time and O(1) space.</p><p><strong>Input:</strong> nums = [1,1,2,3,3,4,4,8,8]</p><p><strong>Output: </strong>2</p><blockquote><strong>Solution:</strong></blockquote><h3>Brute Force Approach:</h3><p><strong>Intuition:</strong></p><p>By reading the problem statement, the first thing that comes to mind is: since the array is sorted, duplicate elements will always appear next to each other.</p><p>So, if an element does not match its left or right neighbour, that element must be the single one.</p><p><strong>Steps:</strong></p><p>1.Traverse the array one by one.</p><p>2.For the first element, check only its right neighbour.</p><p>3.For the last element, check only its left neighbour.</p><p>4.For all other elements, check both neighbours.</p><p>5.Return the element that doesn’t match either neighbour.</p><p><strong>Code:</strong></p><pre>def singleNonDuplicate(nums: List[int]) -&gt; int: <br>   n = len(nums)<br>   for i in range(0, n):<br>      if i == 0:<br>         if nums[i] != nums[i + 1]:<br>            return nums[i]<br>      elif i == n - 1:<br>         if nums[i] != nums[i - 1]:<br>            return nums[i]<br>      else:<br>         if nums[i] != nums[i + 1] and nums[i] != nums[i - 1]:<br>            return nums[i]<br>   return -1</pre><p><strong>Analysis:</strong></p><p><strong>Time Complexity: O(N)</strong> where N is the number of elements in the given array.</p><p><strong>Space Complexity: O(1)</strong> as we are not using any extra space.</p><p>The solution looks simple, but the problem specifically asks us to solve it in <strong>O(log n) time</strong> and <strong>O(1) space</strong>. This means our brute force approach won’t be enough, and we need to think of a more efficient method</p><h3><strong>Optimized Approach:</strong></h3><p><strong>Intuition:</strong></p><p>Whenever we see a sorted array and the problem demands O(log n) time, binary search should immediately come to mind.</p><p>But the real question is:</p><p><strong>how do we apply binary search here?</strong></p><p><strong>What condition tells us whether to move left or right?</strong></p><p>By closely observing the pattern, we notice that in a perfectly paired array, every duplicate pair starts at an even index.</p><p>for example, the pairs (1,1), (2,2), (3,3) would occupy indices (0,1), (2,3), (4,5), and so on.</p><p>However, once a single non-duplicate element is inserted, this pairing pattern breaks: all pairs after the single element will start at an odd index instead of an even one.</p><p>This observation becomes the key to our binary search.</p><p>If we find a pair that starts at an even index, the single element must lie to the right;</p><p>but if a pair starts at an odd index, then the single element must lie to the left.</p><p>Using this logic, we can narrow down the search space until we eventually find the single element.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*tsvqK97MI6x2cSGHvVL2Ag.png" /><figcaption>Iterations</figcaption></figure><p><strong>Steps:</strong></p><p>1. Initialize two pointers left and right to the start and end of the array respectively.</p><p>2. While left is less than right, do the following:</p><ul><li><strong>a.</strong> Calculate the mid index as the average of left and right.</li><li><strong>b.</strong> If mid is odd, decrement it by 1 to make it even.</li><li><strong>c.</strong> If the element at mid position is equal to the element at mid + 1 position, it means the single element is in the right half of the array. So, update left to mid + 2.</li><li><strong>d.</strong> Otherwise, the single element is in the left half of the array. So, update right to mid.</li></ul><p>3. When left is equal to right, return the element at the left index as it will be the single element.</p><p><strong>Code:</strong></p><pre>def singleNonDuplicate(nums: List[int]) -&gt; int:<br>        <br>   left, right = 0, len(nums) - 1<br>   while left &lt; right:<br>      mid = (left + right)//2<br>      if mid % 2 == 1:<br>         mid -= 1<br>    <br>      if nums[mid] == nums[mid+1]:<br>         left = mid + 2<br>      else:<br>         right = mid<br>   return nums[left]</pre><p><strong>Analysis:</strong></p><p><strong>Time Complexity: O(log N)</strong> where N is the number of elements in the given array.</p><p><strong>Space Complexity: O(1) </strong>as we are not using any extra space.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=231d0704e5e8" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>