<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0">
  <channel>
    <title>Tech for good</title>
    <link>https://dianakang.tistory.com/</link>
    <description></description>
    <language>ko</language>
    <pubDate>Sun, 5 Apr 2026 16:30:29 +0900</pubDate>
    <generator>TISTORY</generator>
    <ttl>100</ttl>
    <managingEditor>Diana Kang</managingEditor>
    <image>
      <title>Tech for good</title>
      <url>https://tistory1.daumcdn.net/tistory/4499463/attach/eba4188ef599458cafa699681ceb0eee</url>
      <link>https://dianakang.tistory.com</link>
    </image>
    <item>
      <title>[Leetcode/Binary Tree, CBT, PBT] 222. Count Complete Tree Nodes</title>
      <link>https://dianakang.tistory.com/222</link>
      <description>&lt;p&gt;&lt;figure class=&quot;imageblock alignCenter&quot; data-ke-mobileStyle=&quot;widthOrigin&quot; data-filename=&quot;Screenshot 2025-11-21 at 1.10.11 PM.png&quot; data-origin-width=&quot;551&quot; data-origin-height=&quot;790&quot;&gt;&lt;span data-url=&quot;https://blog.kakaocdn.net/dn/mnlfu/dJMcahbKTIl/Sx0XHkZbKkZu5j6IgYB9cK/img.png&quot; data-phocus=&quot;https://blog.kakaocdn.net/dn/mnlfu/dJMcahbKTIl/Sx0XHkZbKkZu5j6IgYB9cK/img.png&quot;&gt;&lt;img src=&quot;https://blog.kakaocdn.net/dn/mnlfu/dJMcahbKTIl/Sx0XHkZbKkZu5j6IgYB9cK/img.png&quot; srcset=&quot;https://img1.daumcdn.net/thumb/R1280x0/?scode=mtistory2&amp;fname=https%3A%2F%2Fblog.kakaocdn.net%2Fdn%2Fmnlfu%2FdJMcahbKTIl%2FSx0XHkZbKkZu5j6IgYB9cK%2Fimg.png&quot; onerror=&quot;this.onerror=null; this.src='//t1.daumcdn.net/tistory_admin/static/images/no-image-v1.png'; this.srcset='//t1.daumcdn.net/tistory_admin/static/images/no-image-v1.png';&quot; loading=&quot;lazy&quot; width=&quot;551&quot; height=&quot;790&quot; data-filename=&quot;Screenshot 2025-11-21 at 1.10.11 PM.png&quot; data-origin-width=&quot;551&quot; data-origin-height=&quot;790&quot;/&gt;&lt;/span&gt;&lt;/figure&gt;
&lt;/p&gt;
&lt;p data-ke-size=&quot;size16&quot;&gt;&amp;nbsp;&lt;/p&gt;
&lt;p data-ke-size=&quot;size16&quot;&gt;&amp;nbsp;&lt;/p&gt;
&lt;h3 data-ke-size=&quot;size23&quot;&gt;&lt;b&gt;Completely Binary Tree (CBT)&lt;/b&gt;&lt;/h3&gt;
&lt;p&gt;&lt;figure class=&quot;imageblock alignCenter&quot; data-ke-mobileStyle=&quot;widthOrigin&quot; data-origin-width=&quot;786&quot; data-origin-height=&quot;408&quot;&gt;&lt;span data-url=&quot;https://blog.kakaocdn.net/dn/mRNNC/dJMcacO29G8/efvVzPw88qdGo66j8DxC7k/img.png&quot; data-phocus=&quot;https://blog.kakaocdn.net/dn/mRNNC/dJMcacO29G8/efvVzPw88qdGo66j8DxC7k/img.png&quot;&gt;&lt;img src=&quot;https://blog.kakaocdn.net/dn/mRNNC/dJMcacO29G8/efvVzPw88qdGo66j8DxC7k/img.png&quot; srcset=&quot;https://img1.daumcdn.net/thumb/R1280x0/?scode=mtistory2&amp;fname=https%3A%2F%2Fblog.kakaocdn.net%2Fdn%2FmRNNC%2FdJMcacO29G8%2FefvVzPw88qdGo66j8DxC7k%2Fimg.png&quot; onerror=&quot;this.onerror=null; this.src='//t1.daumcdn.net/tistory_admin/static/images/no-image-v1.png'; this.srcset='//t1.daumcdn.net/tistory_admin/static/images/no-image-v1.png';&quot; loading=&quot;lazy&quot; width=&quot;624&quot; height=&quot;324&quot; data-origin-width=&quot;786&quot; data-origin-height=&quot;408&quot;/&gt;&lt;/span&gt;&lt;/figure&gt;
&lt;/p&gt;
&lt;ul style=&quot;list-style-type: disc;&quot; data-ke-list-type=&quot;disc&quot;&gt;
&lt;li&gt;&lt;span style=&quot;color: #000000;&quot;&gt;Completely Binary Tree: CBT; Perfect Binary Tree: PBT&lt;/span&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;figure class=&quot;imageblock alignCenter&quot; data-ke-mobileStyle=&quot;widthOrigin&quot; data-filename=&quot;Screenshot 2025-11-21 at 1.15.07 PM.png&quot; data-origin-width=&quot;765&quot; data-origin-height=&quot;381&quot;&gt;&lt;span data-url=&quot;https://blog.kakaocdn.net/dn/cHncLo/dJMcaaX0i9p/VddosbzUFmOHXsthUIkwf0/img.png&quot; data-phocus=&quot;https://blog.kakaocdn.net/dn/cHncLo/dJMcaaX0i9p/VddosbzUFmOHXsthUIkwf0/img.png&quot;&gt;&lt;img src=&quot;https://blog.kakaocdn.net/dn/cHncLo/dJMcaaX0i9p/VddosbzUFmOHXsthUIkwf0/img.png&quot; srcset=&quot;https://img1.daumcdn.net/thumb/R1280x0/?scode=mtistory2&amp;fname=https%3A%2F%2Fblog.kakaocdn.net%2Fdn%2FcHncLo%2FdJMcaaX0i9p%2FVddosbzUFmOHXsthUIkwf0%2Fimg.png&quot; onerror=&quot;this.onerror=null; this.src='//t1.daumcdn.net/tistory_admin/static/images/no-image-v1.png'; this.srcset='//t1.daumcdn.net/tistory_admin/static/images/no-image-v1.png';&quot; loading=&quot;lazy&quot; width=&quot;765&quot; height=&quot;381&quot; data-filename=&quot;Screenshot 2025-11-21 at 1.15.07 PM.png&quot; data-origin-width=&quot;765&quot; data-origin-height=&quot;381&quot;/&gt;&lt;/span&gt;&lt;/figure&gt;
&lt;/p&gt;
&lt;hr contenteditable=&quot;false&quot; data-ke-type=&quot;horizontalRule&quot; data-ke-style=&quot;style1&quot; /&gt;
&lt;p data-ke-size=&quot;size16&quot;&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;figure class=&quot;imageblock alignCenter&quot; data-ke-mobileStyle=&quot;widthOrigin&quot; data-filename=&quot;Screenshot 2025-11-21 at 1.14.33 PM.png&quot; data-origin-width=&quot;725&quot; data-origin-height=&quot;381&quot;&gt;&lt;span data-url=&quot;https://blog.kakaocdn.net/dn/bkkfC7/dJMcahW8hgV/COrTkD4OPLRiKNIiTAEw91/img.png&quot; data-phocus=&quot;https://blog.kakaocdn.net/dn/bkkfC7/dJMcahW8hgV/COrTkD4OPLRiKNIiTAEw91/img.png&quot;&gt;&lt;img src=&quot;https://blog.kakaocdn.net/dn/bkkfC7/dJMcahW8hgV/COrTkD4OPLRiKNIiTAEw91/img.png&quot; srcset=&quot;https://img1.daumcdn.net/thumb/R1280x0/?scode=mtistory2&amp;fname=https%3A%2F%2Fblog.kakaocdn.net%2Fdn%2FbkkfC7%2FdJMcahW8hgV%2FCOrTkD4OPLRiKNIiTAEw91%2Fimg.png&quot; onerror=&quot;this.onerror=null; this.src='//t1.daumcdn.net/tistory_admin/static/images/no-image-v1.png'; this.srcset='//t1.daumcdn.net/tistory_admin/static/images/no-image-v1.png';&quot; loading=&quot;lazy&quot; width=&quot;725&quot; height=&quot;381&quot; data-filename=&quot;Screenshot 2025-11-21 at 1.14.33 PM.png&quot; data-origin-width=&quot;725&quot; data-origin-height=&quot;381&quot;/&gt;&lt;/span&gt;&lt;/figure&gt;
&lt;figure class=&quot;imageblock alignCenter&quot; data-ke-mobileStyle=&quot;widthOrigin&quot; data-filename=&quot;Screenshot 2025-11-21 at 1.16.20 PM.png&quot; data-origin-width=&quot;782&quot; data-origin-height=&quot;381&quot;&gt;&lt;span data-url=&quot;https://blog.kakaocdn.net/dn/ceLBQu/dJMcabJnLkm/LvyepJNK2FDKk3NUKXKFV1/img.png&quot; data-phocus=&quot;https://blog.kakaocdn.net/dn/ceLBQu/dJMcabJnLkm/LvyepJNK2FDKk3NUKXKFV1/img.png&quot;&gt;&lt;img src=&quot;https://blog.kakaocdn.net/dn/ceLBQu/dJMcabJnLkm/LvyepJNK2FDKk3NUKXKFV1/img.png&quot; srcset=&quot;https://img1.daumcdn.net/thumb/R1280x0/?scode=mtistory2&amp;fname=https%3A%2F%2Fblog.kakaocdn.net%2Fdn%2FceLBQu%2FdJMcabJnLkm%2FLvyepJNK2FDKk3NUKXKFV1%2Fimg.png&quot; onerror=&quot;this.onerror=null; this.src='//t1.daumcdn.net/tistory_admin/static/images/no-image-v1.png'; this.srcset='//t1.daumcdn.net/tistory_admin/static/images/no-image-v1.png';&quot; loading=&quot;lazy&quot; width=&quot;782&quot; height=&quot;381&quot; data-filename=&quot;Screenshot 2025-11-21 at 1.16.20 PM.png&quot; data-origin-width=&quot;782&quot; data-origin-height=&quot;381&quot;/&gt;&lt;/span&gt;&lt;/figure&gt;
&lt;figure class=&quot;imageblock alignCenter&quot; data-ke-mobileStyle=&quot;widthOrigin&quot; data-filename=&quot;Screenshot 2025-11-21 at 1.17.28 PM.png&quot; data-origin-width=&quot;726&quot; data-origin-height=&quot;413&quot;&gt;&lt;span data-url=&quot;https://blog.kakaocdn.net/dn/ZptdL/dJMcacO29Hz/wFXdEO8lsonbmJlksa1a0K/img.png&quot; data-phocus=&quot;https://blog.kakaocdn.net/dn/ZptdL/dJMcacO29Hz/wFXdEO8lsonbmJlksa1a0K/img.png&quot; data-alt=&quot;method 2-case 1&quot;&gt;&lt;img src=&quot;https://blog.kakaocdn.net/dn/ZptdL/dJMcacO29Hz/wFXdEO8lsonbmJlksa1a0K/img.png&quot; srcset=&quot;https://img1.daumcdn.net/thumb/R1280x0/?scode=mtistory2&amp;fname=https%3A%2F%2Fblog.kakaocdn.net%2Fdn%2FZptdL%2FdJMcacO29Hz%2FwFXdEO8lsonbmJlksa1a0K%2Fimg.png&quot; onerror=&quot;this.onerror=null; this.src='//t1.daumcdn.net/tistory_admin/static/images/no-image-v1.png'; this.srcset='//t1.daumcdn.net/tistory_admin/static/images/no-image-v1.png';&quot; loading=&quot;lazy&quot; width=&quot;726&quot; height=&quot;413&quot; data-filename=&quot;Screenshot 2025-11-21 at 1.17.28 PM.png&quot; data-origin-width=&quot;726&quot; data-origin-height=&quot;413&quot;/&gt;&lt;/span&gt;&lt;figcaption&gt;method 2-case 1&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;/p&gt;
&lt;hr contenteditable=&quot;false&quot; data-ke-type=&quot;horizontalRule&quot; data-ke-style=&quot;style6&quot; /&gt;
&lt;h3 data-ke-size=&quot;size23&quot;&gt;&lt;b&gt;Not-efficient approach - &lt;b&gt;O(n)&lt;/b&gt;&lt;/b&gt;&lt;/h3&gt;
&lt;pre id=&quot;code_1763748648366&quot; class=&quot;python&quot; data-ke-language=&quot;python&quot; data-ke-type=&quot;codeblock&quot;&gt;&lt;code&gt;# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution:
    def countNodes(self, root: Optional[TreeNode]) -&amp;gt; int:
        if not root:
            return 0

        return self.countNodes(root.left) + self.countNodes(root.right) + 1&lt;/code&gt;&lt;/pre&gt;
&lt;p data-ke-size=&quot;size16&quot;&gt;&amp;nbsp;&lt;/p&gt;
&lt;h3 style=&quot;color: #000000; text-align: start;&quot; data-ke-size=&quot;size23&quot;&gt;&lt;b&gt;Efficient approach - &lt;b&gt;O(log2n)&lt;span&gt;&amp;nbsp;&lt;/span&gt;&lt;/b&gt;&lt;/b&gt;&lt;/h3&gt;
&lt;pre id=&quot;code_1763760743974&quot; class=&quot;python&quot; data-ke-language=&quot;python&quot; data-ke-type=&quot;codeblock&quot;&gt;&lt;code&gt;# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution:
    def countNodes(self, root: Optional[TreeNode]) -&amp;gt; int:
        if not root:
            return 0
        
        def lheight(node):
            if not node:
                return 0
            return 1 + lheight(node.left)

        def rheight(node):
            if not node:
                return 0
            return 1 + rheight(node.right)
        
        l, r = lheight(root), rheight(root)

        # if it's perfectly balanced
        if l == r:
            return (2**l) - 1
        else:
            return 1 + self.countNodes(root.left) + self.countNodes(root.right)&lt;/code&gt;&lt;/pre&gt;</description>
      <category>IT/Computer Science</category>
      <category>binary Tree</category>
      <category>CBT</category>
      <category>PBT</category>
      <author>Diana Kang</author>
      <guid isPermaLink="true">https://dianakang.tistory.com/222</guid>
      <comments>https://dianakang.tistory.com/222#entry222comment</comments>
      <pubDate>Sat, 22 Nov 2025 06:34:48 +0900</pubDate>
    </item>
    <item>
      <title>[Leetcode/Array, Heap (Priority Queue)] 973. K Closest Points to Origin</title>
      <link>https://dianakang.tistory.com/221</link>
      <description>&lt;p&gt;&lt;figure class=&quot;imageblock alignCenter&quot; data-ke-mobileStyle=&quot;widthOrigin&quot; data-filename=&quot;Screenshot 2025-08-19 at 6.01.55 PM.png&quot; data-origin-width=&quot;1310&quot; data-origin-height=&quot;1316&quot;&gt;&lt;span data-url=&quot;https://blog.kakaocdn.net/dn/bOYqnH/btsPX0CDWaT/h1qK3NpjVIWinevWpBPfU0/img.png&quot; data-phocus=&quot;https://blog.kakaocdn.net/dn/bOYqnH/btsPX0CDWaT/h1qK3NpjVIWinevWpBPfU0/img.png&quot;&gt;&lt;img src=&quot;https://blog.kakaocdn.net/dn/bOYqnH/btsPX0CDWaT/h1qK3NpjVIWinevWpBPfU0/img.png&quot; srcset=&quot;https://img1.daumcdn.net/thumb/R1280x0/?scode=mtistory2&amp;fname=https%3A%2F%2Fblog.kakaocdn.net%2Fdn%2FbOYqnH%2FbtsPX0CDWaT%2Fh1qK3NpjVIWinevWpBPfU0%2Fimg.png&quot; onerror=&quot;this.onerror=null; this.src='//t1.daumcdn.net/tistory_admin/static/images/no-image-v1.png'; this.srcset='//t1.daumcdn.net/tistory_admin/static/images/no-image-v1.png';&quot; loading=&quot;lazy&quot; width=&quot;1310&quot; height=&quot;1316&quot; data-filename=&quot;Screenshot 2025-08-19 at 6.01.55 PM.png&quot; data-origin-width=&quot;1310&quot; data-origin-height=&quot;1316&quot;/&gt;&lt;/span&gt;&lt;/figure&gt;
&lt;figure class=&quot;imageblock alignCenter&quot; data-ke-mobileStyle=&quot;widthOrigin&quot; data-filename=&quot;Screenshot 2025-08-19 at 6.02.17 PM.png&quot; data-origin-width=&quot;1310&quot; data-origin-height=&quot;520&quot;&gt;&lt;span data-url=&quot;https://blog.kakaocdn.net/dn/7EB01/btsPYNpnmkT/vwanPUzl1M6k04raCIitE1/img.png&quot; data-phocus=&quot;https://blog.kakaocdn.net/dn/7EB01/btsPYNpnmkT/vwanPUzl1M6k04raCIitE1/img.png&quot;&gt;&lt;img src=&quot;https://blog.kakaocdn.net/dn/7EB01/btsPYNpnmkT/vwanPUzl1M6k04raCIitE1/img.png&quot; srcset=&quot;https://img1.daumcdn.net/thumb/R1280x0/?scode=mtistory2&amp;fname=https%3A%2F%2Fblog.kakaocdn.net%2Fdn%2F7EB01%2FbtsPYNpnmkT%2FvwanPUzl1M6k04raCIitE1%2Fimg.png&quot; onerror=&quot;this.onerror=null; this.src='//t1.daumcdn.net/tistory_admin/static/images/no-image-v1.png'; this.srcset='//t1.daumcdn.net/tistory_admin/static/images/no-image-v1.png';&quot; loading=&quot;lazy&quot; width=&quot;1310&quot; height=&quot;520&quot; data-filename=&quot;Screenshot 2025-08-19 at 6.02.17 PM.png&quot; data-origin-width=&quot;1310&quot; data-origin-height=&quot;520&quot;/&gt;&lt;/span&gt;&lt;/figure&gt;
&lt;/p&gt;
&lt;pre id=&quot;code_1755655455771&quot; class=&quot;python&quot; data-ke-language=&quot;python&quot; data-ke-type=&quot;codeblock&quot;&gt;&lt;code&gt;class Solution:
    def kClosest(self, points: List[List[int]], k: int) -&amp;gt; List[List[int]]:
        pq = []

        for x, y in points:
            d = x * x + y * y
            pq.append((d, (x,y))) # make it a tuple

        heapify(pq)

        res = []

        for i in range(k):
            res.append(heappop(pq)[1]) # heappop(pq) = (d, (x, y))
        
        return res&lt;/code&gt;&lt;/pre&gt;
&lt;p data-ke-size=&quot;size16&quot;&gt;&amp;nbsp;&lt;/p&gt;</description>
      <category>IT/Computer Science</category>
      <category>array</category>
      <category>heap</category>
      <category>Priority Queue</category>
      <author>Diana Kang</author>
      <guid isPermaLink="true">https://dianakang.tistory.com/221</guid>
      <comments>https://dianakang.tistory.com/221#entry221comment</comments>
      <pubDate>Wed, 20 Aug 2025 11:05:38 +0900</pubDate>
    </item>
    <item>
      <title>[Leetcode/Array, Heap (Priority Queue) 1046. Last Stone Weight</title>
      <link>https://dianakang.tistory.com/220</link>
      <description>&lt;p&gt;&lt;figure class=&quot;imageblock alignCenter&quot; data-ke-mobileStyle=&quot;widthOrigin&quot; data-filename=&quot;Screenshot 2025-08-18 at 7.05.52 PM.png&quot; data-origin-width=&quot;1478&quot; data-origin-height=&quot;1278&quot;&gt;&lt;span data-url=&quot;https://blog.kakaocdn.net/dn/cAKMjm/btsPUPBHUHM/epWLWtznbGW5U0KK29X9yK/img.png&quot; data-phocus=&quot;https://blog.kakaocdn.net/dn/cAKMjm/btsPUPBHUHM/epWLWtznbGW5U0KK29X9yK/img.png&quot;&gt;&lt;img src=&quot;https://blog.kakaocdn.net/dn/cAKMjm/btsPUPBHUHM/epWLWtznbGW5U0KK29X9yK/img.png&quot; srcset=&quot;https://img1.daumcdn.net/thumb/R1280x0/?scode=mtistory2&amp;fname=https%3A%2F%2Fblog.kakaocdn.net%2Fdn%2FcAKMjm%2FbtsPUPBHUHM%2FepWLWtznbGW5U0KK29X9yK%2Fimg.png&quot; onerror=&quot;this.onerror=null; this.src='//t1.daumcdn.net/tistory_admin/static/images/no-image-v1.png'; this.srcset='//t1.daumcdn.net/tistory_admin/static/images/no-image-v1.png';&quot; loading=&quot;lazy&quot; width=&quot;1478&quot; height=&quot;1278&quot; data-filename=&quot;Screenshot 2025-08-18 at 7.05.52 PM.png&quot; data-origin-width=&quot;1478&quot; data-origin-height=&quot;1278&quot;/&gt;&lt;/span&gt;&lt;/figure&gt;
&lt;/p&gt;
&lt;pre id=&quot;code_1755560970527&quot; class=&quot;python&quot; data-ke-language=&quot;python&quot; data-ke-type=&quot;codeblock&quot;&gt;&lt;code&gt;class Solution:
    def lastStoneWeight(self, stones: List[int]) -&amp;gt; int:
        # what is heap? =&amp;gt; find min/max using push() or pop()
        # it allows handling new value effeciently when we even insert new values.
        pq = [-x for x in stones] 
        # pq = [-2, -7, -4, -1, -8, -1]
        
        heapify(pq) # heapify() -&amp;gt; make it into binarry tree for efficiency (only half-through traversal)

        while len(pq) &amp;gt; 1:
            heaviest = -1 * heappop(pq)
            second_heaviest = -1 * heappop(pq)

            if heaviest != second_heaviest:
                new_weight = (heaviest - second_heaviest)
                heappush(pq, -1 * new_weight)
            
        return -1 * pq[0] if len(pq) &amp;gt; 0 else 0&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;figure class=&quot;imagegridblock&quot;&gt;
  &lt;div class=&quot;image-container&quot;&gt;&lt;span data-url=&quot;https://blog.kakaocdn.net/dn/nwAUR/btsPXtYLAj2/gkDrYwkWiCtxgncWaJhNw1/img.png&quot; data-phocus=&quot;https://blog.kakaocdn.net/dn/nwAUR/btsPXtYLAj2/gkDrYwkWiCtxgncWaJhNw1/img.png&quot; data-filename=&quot;Screenshot 2025-08-18 at 5.52.13 PM.png&quot; data-origin-height=&quot;748&quot; data-origin-width=&quot;774&quot; data-is-animation=&quot;false&quot; style=&quot;width: 49.4186%; margin-right: 10px;&quot; data-widthpercent=&quot;50&quot;&gt;&lt;img src=&quot;https://blog.kakaocdn.net/dn/nwAUR/btsPXtYLAj2/gkDrYwkWiCtxgncWaJhNw1/img.png&quot; srcset=&quot;https://img1.daumcdn.net/thumb/R1280x0/?scode=mtistory2&amp;amp;fname=https%3A%2F%2Fblog.kakaocdn.net%2Fdn%2FnwAUR%2FbtsPXtYLAj2%2FgkDrYwkWiCtxgncWaJhNw1%2Fimg.png&quot; onerror=&quot;this.onerror=null; this.src='//t1.daumcdn.net/tistory_admin/static/images/no-image-v1.png'; this.srcset='//t1.daumcdn.net/tistory_admin/static/images/no-image-v1.png';&quot; loading=&quot;lazy&quot; width=&quot;774&quot; height=&quot;748&quot;/&gt;&lt;/span&gt;&lt;span data-url=&quot;https://blog.kakaocdn.net/dn/bG5epn/btsPUdv91f6/VmbrnSTlbazk7qrLcCpDi0/img.png&quot; data-phocus=&quot;https://blog.kakaocdn.net/dn/bG5epn/btsPUdv91f6/VmbrnSTlbazk7qrLcCpDi0/img.png&quot; data-is-animation=&quot;false&quot; data-origin-width=&quot;774&quot; data-origin-height=&quot;748&quot; data-filename=&quot;Screenshot 2025-08-18 at 5.54.29 PM.png&quot; style=&quot;width: 49.4186%;&quot; data-widthpercent=&quot;50&quot;&gt;&lt;img src=&quot;https://blog.kakaocdn.net/dn/bG5epn/btsPUdv91f6/VmbrnSTlbazk7qrLcCpDi0/img.png&quot; srcset=&quot;https://img1.daumcdn.net/thumb/R1280x0/?scode=mtistory2&amp;amp;fname=https%3A%2F%2Fblog.kakaocdn.net%2Fdn%2FbG5epn%2FbtsPUdv91f6%2FVmbrnSTlbazk7qrLcCpDi0%2Fimg.png&quot; onerror=&quot;this.onerror=null; this.src='//t1.daumcdn.net/tistory_admin/static/images/no-image-v1.png'; this.srcset='//t1.daumcdn.net/tistory_admin/static/images/no-image-v1.png';&quot; loading=&quot;lazy&quot; width=&quot;774&quot; height=&quot;748&quot;/&gt;&lt;/span&gt;&lt;/div&gt;
&lt;/figure&gt;
&lt;/p&gt;</description>
      <category>IT/Computer Science</category>
      <category>array</category>
      <category>heap</category>
      <category>Priority Queue</category>
      <author>Diana Kang</author>
      <guid isPermaLink="true">https://dianakang.tistory.com/220</guid>
      <comments>https://dianakang.tistory.com/220#entry220comment</comments>
      <pubDate>Tue, 19 Aug 2025 08:52:43 +0900</pubDate>
    </item>
    <item>
      <title>[Codepath/BFS, Graph] Count Connected Components</title>
      <link>https://dianakang.tistory.com/219</link>
      <description>&lt;p&gt;&lt;figure class=&quot;imageblock alignCenter&quot; data-ke-mobileStyle=&quot;widthOrigin&quot; data-filename=&quot;Screenshot 2025-08-11 at 12.11.27 PM.png&quot; data-origin-width=&quot;579&quot; data-origin-height=&quot;370&quot;&gt;&lt;span data-url=&quot;https://blog.kakaocdn.net/dn/kFXzM/btsPN28P3LS/fFxVCDUgcchsGniXxPbUD1/img.png&quot; data-phocus=&quot;https://blog.kakaocdn.net/dn/kFXzM/btsPN28P3LS/fFxVCDUgcchsGniXxPbUD1/img.png&quot;&gt;&lt;img src=&quot;https://blog.kakaocdn.net/dn/kFXzM/btsPN28P3LS/fFxVCDUgcchsGniXxPbUD1/img.png&quot; srcset=&quot;https://img1.daumcdn.net/thumb/R1280x0/?scode=mtistory2&amp;fname=https%3A%2F%2Fblog.kakaocdn.net%2Fdn%2FkFXzM%2FbtsPN28P3LS%2FfFxVCDUgcchsGniXxPbUD1%2Fimg.png&quot; onerror=&quot;this.onerror=null; this.src='//t1.daumcdn.net/tistory_admin/static/images/no-image-v1.png'; this.srcset='//t1.daumcdn.net/tistory_admin/static/images/no-image-v1.png';&quot; loading=&quot;lazy&quot; width=&quot;579&quot; height=&quot;370&quot; data-filename=&quot;Screenshot 2025-08-11 at 12.11.27 PM.png&quot; data-origin-width=&quot;579&quot; data-origin-height=&quot;370&quot;/&gt;&lt;/span&gt;&lt;/figure&gt;
&lt;/p&gt;
&lt;pre id=&quot;code_1754928699111&quot; class=&quot;python&quot; data-ke-language=&quot;python&quot; data-ke-type=&quot;codeblock&quot;&gt;&lt;code&gt;def count_components(adjacency_dict):
    # Write your code here
    visited = set()
    cnt = 0
    
    def dfs(node):
        visited.add(node)
        for neighbor in adjacency_dict[node]:
            if neighbor not in visited:
                dfs(neighbor)
    
    for node in adjacency_dict:
        if node not in visited:
            dfs(node)
            cnt += 1
    
    return cnt&lt;/code&gt;&lt;/pre&gt;</description>
      <category>IT/Computer Science</category>
      <category>BFS</category>
      <category>graph</category>
      <category>set()</category>
      <author>Diana Kang</author>
      <guid isPermaLink="true">https://dianakang.tistory.com/219</guid>
      <comments>https://dianakang.tistory.com/219#entry219comment</comments>
      <pubDate>Tue, 12 Aug 2025 01:11:57 +0900</pubDate>
    </item>
    <item>
      <title>[Codepath/BFS, Graph] Find Path</title>
      <link>https://dianakang.tistory.com/218</link>
      <description>&lt;p&gt;&lt;figure class=&quot;imageblock alignCenter&quot; data-ke-mobileStyle=&quot;widthOrigin&quot; data-filename=&quot;Screenshot 2025-08-11 at 11.51.46 AM.png&quot; data-origin-width=&quot;577&quot; data-origin-height=&quot;543&quot;&gt;&lt;span data-url=&quot;https://blog.kakaocdn.net/dn/dKvOHk/btsPNkBEUIO/kdVS7t7KPuDwZsd6SlonT1/img.png&quot; data-phocus=&quot;https://blog.kakaocdn.net/dn/dKvOHk/btsPNkBEUIO/kdVS7t7KPuDwZsd6SlonT1/img.png&quot;&gt;&lt;img src=&quot;https://blog.kakaocdn.net/dn/dKvOHk/btsPNkBEUIO/kdVS7t7KPuDwZsd6SlonT1/img.png&quot; srcset=&quot;https://img1.daumcdn.net/thumb/R1280x0/?scode=mtistory2&amp;fname=https%3A%2F%2Fblog.kakaocdn.net%2Fdn%2FdKvOHk%2FbtsPNkBEUIO%2FkdVS7t7KPuDwZsd6SlonT1%2Fimg.png&quot; onerror=&quot;this.onerror=null; this.src='//t1.daumcdn.net/tistory_admin/static/images/no-image-v1.png'; this.srcset='//t1.daumcdn.net/tistory_admin/static/images/no-image-v1.png';&quot; loading=&quot;lazy&quot; width=&quot;577&quot; height=&quot;543&quot; data-filename=&quot;Screenshot 2025-08-11 at 11.51.46 AM.png&quot; data-origin-width=&quot;577&quot; data-origin-height=&quot;543&quot;/&gt;&lt;/span&gt;&lt;/figure&gt;
&lt;/p&gt;
&lt;pre id=&quot;code_1754927521391&quot; class=&quot;python&quot; data-ke-language=&quot;python&quot; data-ke-type=&quot;codeblock&quot;&gt;&lt;code&gt;from collections import deque, defaultdict

def has_path(adjacency_dict, start, destination):
    # Write your code here
    if start == destination:
        return True
        
    graph = defaultdict(list, adjacency_dict)
    
    q = deque([start])
    visited = set([start])

    while q:
        curr = q.popleft()
        
        if curr == destination:
            return True
        
        for neighbor in graph[curr]:
            if neighbor not in visited:
                visited.add(neighbor)
                q.append(neighbor)
    
    return False&lt;/code&gt;&lt;/pre&gt;</description>
      <category>IT/Computer Science</category>
      <category>defaultdict()</category>
      <category>deque()</category>
      <category>dfs</category>
      <author>Diana Kang</author>
      <guid isPermaLink="true">https://dianakang.tistory.com/218</guid>
      <comments>https://dianakang.tistory.com/218#entry218comment</comments>
      <pubDate>Tue, 12 Aug 2025 00:53:42 +0900</pubDate>
    </item>
    <item>
      <title>[Leetcode/DFS, BFS, Graph, Matrix] 200. Number of Islands</title>
      <link>https://dianakang.tistory.com/217</link>
      <description>&lt;p&gt;&lt;figure class=&quot;imageblock alignCenter&quot; data-ke-mobileStyle=&quot;widthOrigin&quot; data-filename=&quot;Screenshot 2025-08-08 at 5.44.54 PM.png&quot; data-origin-width=&quot;1812&quot; data-origin-height=&quot;1272&quot;&gt;&lt;span data-url=&quot;https://blog.kakaocdn.net/dn/bUmj7I/btsPMO3lkES/XyxIqebVEsiYCOWpUKpJm1/img.png&quot; data-phocus=&quot;https://blog.kakaocdn.net/dn/bUmj7I/btsPMO3lkES/XyxIqebVEsiYCOWpUKpJm1/img.png&quot;&gt;&lt;img src=&quot;https://blog.kakaocdn.net/dn/bUmj7I/btsPMO3lkES/XyxIqebVEsiYCOWpUKpJm1/img.png&quot; srcset=&quot;https://img1.daumcdn.net/thumb/R1280x0/?scode=mtistory2&amp;fname=https%3A%2F%2Fblog.kakaocdn.net%2Fdn%2FbUmj7I%2FbtsPMO3lkES%2FXyxIqebVEsiYCOWpUKpJm1%2Fimg.png&quot; onerror=&quot;this.onerror=null; this.src='//t1.daumcdn.net/tistory_admin/static/images/no-image-v1.png'; this.srcset='//t1.daumcdn.net/tistory_admin/static/images/no-image-v1.png';&quot; loading=&quot;lazy&quot; width=&quot;1812&quot; height=&quot;1272&quot; data-filename=&quot;Screenshot 2025-08-08 at 5.44.54 PM.png&quot; data-origin-width=&quot;1812&quot; data-origin-height=&quot;1272&quot;/&gt;&lt;/span&gt;&lt;/figure&gt;
&lt;/p&gt;
&lt;pre id=&quot;code_1754690444889&quot; class=&quot;python&quot; data-ke-language=&quot;python&quot; data-ke-type=&quot;codeblock&quot;&gt;&lt;code&gt;class Solution:
    def numIslands(self, grid: List[List[str]]) -&amp;gt; int:
        n_rows, n_cols = len(grid), len(grid[0])

        res = 0
        visited = set()

        def dfs(r, c):
            visited.add((r,c))

            neighbors = [(r-1, c), (r+1, c), (r, c-1), (r, c+1)]

            # check if visited or not
            for n_r, n_c in neighbors:
                if n_r in range(n_rows) and n_c in range(n_cols):
                    if grid[n_r][n_c] == &quot;1&quot; and (n_r, n_c) not in visited:
                        dfs(n_r, n_c)
        
        # check grid one by one
        for r in range(n_rows):
            for c in range(n_cols):
                # If the cell is land and not visited, it's a new island
                if grid[r][c] == &quot;1&quot; and (r,c) not in visited:
                    # # Explore the entire island using DFS
                    dfs(r,c)
                    # # Increase the island count
                    res += 1

        return res&lt;/code&gt;&lt;/pre&gt;</description>
      <category>IT/Computer Science</category>
      <category>BFS</category>
      <category>dfs</category>
      <category>graph</category>
      <category>Grid</category>
      <category>matrix</category>
      <author>Diana Kang</author>
      <guid isPermaLink="true">https://dianakang.tistory.com/217</guid>
      <comments>https://dianakang.tistory.com/217#entry217comment</comments>
      <pubDate>Sat, 9 Aug 2025 07:00:55 +0900</pubDate>
    </item>
    <item>
      <title>[Leetcode/DFS, BFS, Graph] 1971. Find if Path Exists in Graph</title>
      <link>https://dianakang.tistory.com/216</link>
      <description>&lt;p&gt;&lt;figure class=&quot;imageblock alignCenter&quot; data-ke-mobileStyle=&quot;widthOrigin&quot; data-origin-width=&quot;1424&quot; data-origin-height=&quot;1398&quot;&gt;&lt;span data-url=&quot;https://blog.kakaocdn.net/dn/xphqr/btsPKjJk443/9JFxbFDtKPej2cj3ULuSNk/img.png&quot; data-phocus=&quot;https://blog.kakaocdn.net/dn/xphqr/btsPKjJk443/9JFxbFDtKPej2cj3ULuSNk/img.png&quot;&gt;&lt;img src=&quot;https://blog.kakaocdn.net/dn/xphqr/btsPKjJk443/9JFxbFDtKPej2cj3ULuSNk/img.png&quot; srcset=&quot;https://img1.daumcdn.net/thumb/R1280x0/?scode=mtistory2&amp;fname=https%3A%2F%2Fblog.kakaocdn.net%2Fdn%2Fxphqr%2FbtsPKjJk443%2F9JFxbFDtKPej2cj3ULuSNk%2Fimg.png&quot; onerror=&quot;this.onerror=null; this.src='//t1.daumcdn.net/tistory_admin/static/images/no-image-v1.png'; this.srcset='//t1.daumcdn.net/tistory_admin/static/images/no-image-v1.png';&quot; loading=&quot;lazy&quot; width=&quot;1424&quot; height=&quot;1398&quot; data-origin-width=&quot;1424&quot; data-origin-height=&quot;1398&quot;/&gt;&lt;/span&gt;&lt;/figure&gt;
&lt;figure class=&quot;imageblock alignCenter&quot; data-ke-mobileStyle=&quot;widthOrigin&quot; data-origin-width=&quot;1424&quot; data-origin-height=&quot;516&quot;&gt;&lt;span data-url=&quot;https://blog.kakaocdn.net/dn/BWrWE/btsPLWM6M9m/EfWN6gG2XDkxOrSk53AYjk/img.png&quot; data-phocus=&quot;https://blog.kakaocdn.net/dn/BWrWE/btsPLWM6M9m/EfWN6gG2XDkxOrSk53AYjk/img.png&quot;&gt;&lt;img src=&quot;https://blog.kakaocdn.net/dn/BWrWE/btsPLWM6M9m/EfWN6gG2XDkxOrSk53AYjk/img.png&quot; srcset=&quot;https://img1.daumcdn.net/thumb/R1280x0/?scode=mtistory2&amp;fname=https%3A%2F%2Fblog.kakaocdn.net%2Fdn%2FBWrWE%2FbtsPLWM6M9m%2FEfWN6gG2XDkxOrSk53AYjk%2Fimg.png&quot; onerror=&quot;this.onerror=null; this.src='//t1.daumcdn.net/tistory_admin/static/images/no-image-v1.png'; this.srcset='//t1.daumcdn.net/tistory_admin/static/images/no-image-v1.png';&quot; loading=&quot;lazy&quot; width=&quot;1424&quot; height=&quot;516&quot; data-origin-width=&quot;1424&quot; data-origin-height=&quot;516&quot;/&gt;&lt;/span&gt;&lt;/figure&gt;
&lt;/p&gt;
&lt;pre class=&quot;python&quot; data-ke-type=&quot;codeblock&quot; data-ke-language=&quot;python&quot;&gt;&lt;code&gt;class Solution:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;def validPath(self, n: int, edges: List[List[int]], source: int, destination: int) -&amp;gt; bool:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;# 1. Make a graph
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;## To figure out neighbor using hash-map
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;## 0 =&amp;gt; [1,2]

&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;graph = defaultdict(list)

&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;for u, v in edges:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;graph[u].append(v)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;graph[v].append(u)

&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;# 2. BFS from source
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;q = deque([source])
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;visited = set([source])

&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;while q:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;curr = q.popleft()

&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;if curr == destination:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;return True
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;for neighbor in graph[curr]:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;if neighbor not in visited:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;visited.add(neighbor)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;q.append(neighbor)

&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;return False&lt;/code&gt;&lt;/pre&gt;</description>
      <category>IT/Computer Science</category>
      <category>BFS</category>
      <category>dfs</category>
      <category>graph</category>
      <author>Diana Kang</author>
      <guid isPermaLink="true">https://dianakang.tistory.com/216</guid>
      <comments>https://dianakang.tistory.com/216#entry216comment</comments>
      <pubDate>Fri, 8 Aug 2025 11:05:34 +0900</pubDate>
    </item>
    <item>
      <title>[Leetcode/Array, Hash Table, Graph] 997. Find the Town Judge</title>
      <link>https://dianakang.tistory.com/215</link>
      <description>&lt;p&gt;&lt;figure class=&quot;imageblock alignCenter&quot; data-ke-mobileStyle=&quot;widthOrigin&quot; data-filename=&quot;Screenshot 2025-08-07 at 9.55.09 PM.png&quot; data-origin-width=&quot;1412&quot; data-origin-height=&quot;1398&quot;&gt;&lt;span data-url=&quot;https://blog.kakaocdn.net/dn/bFRbO7/btsPJudKBwN/y1HfkXpmzUYe7FTBjDxx1K/img.png&quot; data-phocus=&quot;https://blog.kakaocdn.net/dn/bFRbO7/btsPJudKBwN/y1HfkXpmzUYe7FTBjDxx1K/img.png&quot;&gt;&lt;img src=&quot;https://blog.kakaocdn.net/dn/bFRbO7/btsPJudKBwN/y1HfkXpmzUYe7FTBjDxx1K/img.png&quot; srcset=&quot;https://img1.daumcdn.net/thumb/R1280x0/?scode=mtistory2&amp;fname=https%3A%2F%2Fblog.kakaocdn.net%2Fdn%2FbFRbO7%2FbtsPJudKBwN%2Fy1HfkXpmzUYe7FTBjDxx1K%2Fimg.png&quot; onerror=&quot;this.onerror=null; this.src='//t1.daumcdn.net/tistory_admin/static/images/no-image-v1.png'; this.srcset='//t1.daumcdn.net/tistory_admin/static/images/no-image-v1.png';&quot; loading=&quot;lazy&quot; width=&quot;1412&quot; height=&quot;1398&quot; data-filename=&quot;Screenshot 2025-08-07 at 9.55.09 PM.png&quot; data-origin-width=&quot;1412&quot; data-origin-height=&quot;1398&quot;/&gt;&lt;/span&gt;&lt;/figure&gt;
&lt;/p&gt;
&lt;ul style=&quot;list-style-type: disc;&quot; data-ke-list-type=&quot;disc&quot;&gt;
&lt;li&gt;&lt;b&gt;Properties 1&lt;/b&gt;
&lt;ul style=&quot;list-style-type: disc;&quot; data-ke-list-type=&quot;disc&quot;&gt;
&lt;li&gt;The town judge trusts nobody.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;&lt;b&gt;Properties 2&lt;/b&gt;
&lt;ul style=&quot;list-style-type: disc;&quot; data-ke-list-type=&quot;disc&quot;&gt;
&lt;li&gt;Everybody (except for the town judge) trusts the town judge.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;&lt;b&gt;Properties 3&lt;/b&gt;
&lt;ul style=&quot;list-style-type: disc;&quot; data-ke-list-type=&quot;disc&quot;&gt;
&lt;li&gt;There is exactly one person that satisfies properties 1 and 2.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;pre id=&quot;code_1754618126680&quot; class=&quot;python&quot; data-ke-language=&quot;python&quot; data-ke-type=&quot;codeblock&quot;&gt;&lt;code&gt;class Solution:
    def findJudge(self, n: int, trust: List[List[int]]) -&amp;gt; int:
        #  To figure out in-degree/out-degree using hash-map
        in_degree = defaultdict(int)
        out_degree = defaultdict(int)
        
        # for p0 = pair(trust)[0], p1 = pair(trust)[1]
        for p0,p1 in trust:
            in_degree[p1] += 1
            out_degree[p0] += 1
        
        for i in range(1, n+1):
            # indegree[i] == n-1 =&amp;gt; properties 2
            # outdegree[i] == 0 =&amp;gt; properties 1
            if in_degree[i] == n-1 and out_degree[i] == 0:
                return i

        return -1&lt;/code&gt;&lt;/pre&gt;
&lt;p data-ke-size=&quot;size16&quot;&gt;&amp;nbsp;&lt;/p&gt;</description>
      <category>IT/Computer Science</category>
      <category>array</category>
      <category>graph</category>
      <category>hash map</category>
      <author>Diana Kang</author>
      <guid isPermaLink="true">https://dianakang.tistory.com/215</guid>
      <comments>https://dianakang.tistory.com/215#entry215comment</comments>
      <pubDate>Fri, 8 Aug 2025 10:59:12 +0900</pubDate>
    </item>
    <item>
      <title>[HackerRank] Inorder Traversal of Binary Tree</title>
      <link>https://dianakang.tistory.com/214</link>
      <description></description>
      <category>IT/Computer Science</category>
      <category>inordertraversal</category>
      <category>leftrootright</category>
      <author>Diana Kang</author>
      <guid isPermaLink="true">https://dianakang.tistory.com/214</guid>
      <comments>https://dianakang.tistory.com/214#entry214comment</comments>
      <pubDate>Mon, 4 Aug 2025 05:49:43 +0900</pubDate>
    </item>
    <item>
      <title>[Leetcode/Tree, Binary Tree, DFS] 572. Subtree of Another Tree</title>
      <link>https://dianakang.tistory.com/213</link>
      <description>&lt;p&gt;&lt;figure class=&quot;imageblock alignCenter&quot; data-ke-mobileStyle=&quot;widthOrigin&quot; data-filename=&quot;Screenshot 2025-07-25 at 3.51.46 PM.png&quot; data-origin-width=&quot;1788&quot; data-origin-height=&quot;1126&quot;&gt;&lt;span data-url=&quot;https://blog.kakaocdn.net/dn/mlCdq/btsPAWNT8mS/WDXEJYRmGxWtc0X4p8QNs1/img.png&quot; data-phocus=&quot;https://blog.kakaocdn.net/dn/mlCdq/btsPAWNT8mS/WDXEJYRmGxWtc0X4p8QNs1/img.png&quot;&gt;&lt;img src=&quot;https://blog.kakaocdn.net/dn/mlCdq/btsPAWNT8mS/WDXEJYRmGxWtc0X4p8QNs1/img.png&quot; srcset=&quot;https://img1.daumcdn.net/thumb/R1280x0/?scode=mtistory2&amp;fname=https%3A%2F%2Fblog.kakaocdn.net%2Fdn%2FmlCdq%2FbtsPAWNT8mS%2FWDXEJYRmGxWtc0X4p8QNs1%2Fimg.png&quot; onerror=&quot;this.onerror=null; this.src='//t1.daumcdn.net/tistory_admin/static/images/no-image-v1.png'; this.srcset='//t1.daumcdn.net/tistory_admin/static/images/no-image-v1.png';&quot; loading=&quot;lazy&quot; width=&quot;1788&quot; height=&quot;1126&quot; data-filename=&quot;Screenshot 2025-07-25 at 3.51.46 PM.png&quot; data-origin-width=&quot;1788&quot; data-origin-height=&quot;1126&quot;/&gt;&lt;/span&gt;&lt;/figure&gt;
&lt;figure class=&quot;imageblock alignCenter&quot; data-ke-mobileStyle=&quot;widthOrigin&quot; data-filename=&quot;Screenshot 2025-07-25 at 3.52.14 PM.png&quot; data-origin-width=&quot;1788&quot; data-origin-height=&quot;1238&quot;&gt;&lt;span data-url=&quot;https://blog.kakaocdn.net/dn/brffpZ/btsPABwomnJ/VoL1wJnGBGuXQ48MaFmsEK/img.png&quot; data-phocus=&quot;https://blog.kakaocdn.net/dn/brffpZ/btsPABwomnJ/VoL1wJnGBGuXQ48MaFmsEK/img.png&quot;&gt;&lt;img src=&quot;https://blog.kakaocdn.net/dn/brffpZ/btsPABwomnJ/VoL1wJnGBGuXQ48MaFmsEK/img.png&quot; srcset=&quot;https://img1.daumcdn.net/thumb/R1280x0/?scode=mtistory2&amp;fname=https%3A%2F%2Fblog.kakaocdn.net%2Fdn%2FbrffpZ%2FbtsPABwomnJ%2FVoL1wJnGBGuXQ48MaFmsEK%2Fimg.png&quot; onerror=&quot;this.onerror=null; this.src='//t1.daumcdn.net/tistory_admin/static/images/no-image-v1.png'; this.srcset='//t1.daumcdn.net/tistory_admin/static/images/no-image-v1.png';&quot; loading=&quot;lazy&quot; width=&quot;1788&quot; height=&quot;1238&quot; data-filename=&quot;Screenshot 2025-07-25 at 3.52.14 PM.png&quot; data-origin-width=&quot;1788&quot; data-origin-height=&quot;1238&quot;/&gt;&lt;/span&gt;&lt;/figure&gt;
&lt;/p&gt;
&lt;pre id=&quot;code_1753473014992&quot; class=&quot;python&quot; data-ke-language=&quot;python&quot; data-ke-type=&quot;codeblock&quot;&gt;&lt;code&gt;# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution:
    def isSubtree(self, root: Optional[TreeNode], subRoot: Optional[TreeNode]) -&amp;gt; bool:
        if not root:
            return False
        return self.sameTree(root, subRoot) or self.isSubtree(root.left, subRoot) or self.isSubtree(root.right, subRoot)
    
    def sameTree(self, root1, root2):
        if not root1 and not root2:
            return True
        if not root1 or not root2:
            return False
        return root1.val == root2.val and self.sameTree(root1.left, root2.left) and self.sameTree(root1.right, root2.right)&lt;/code&gt;&lt;/pre&gt;</description>
      <category>IT/Computer Science</category>
      <category>binary Tree</category>
      <category>dfs</category>
      <category>tree</category>
      <author>Diana Kang</author>
      <guid isPermaLink="true">https://dianakang.tistory.com/213</guid>
      <comments>https://dianakang.tistory.com/213#entry213comment</comments>
      <pubDate>Sat, 26 Jul 2025 04:54:54 +0900</pubDate>
    </item>
  </channel>
</rss>