{"id":94,"date":"2025-05-12T18:18:50","date_gmt":"2025-05-12T12:48:50","guid":{"rendered":"https:\/\/codeanddebug.in\/blog\/?p=94"},"modified":"2025-05-14T12:11:46","modified_gmt":"2025-05-14T06:41:46","slug":"breadth-first-search-in-binary-trees","status":"publish","type":"post","link":"https:\/\/codeanddebug.in\/blog\/breadth-first-search-in-binary-trees\/","title":{"rendered":"Breadth-First Search in Binary Trees: Level-Order Traversal with Python"},"content":{"rendered":"\n<p>Learn how to perform a level-order <strong>(BFS)<\/strong> traversal on a binary tree using Python\u2019s <code>collections.deque<\/code>. This beginner-friendly guide shows you step-by-step code, intuition, and real examples, perfect for LeetCode prep!<\/p>\n\n\n<div style=\"max-width: -moz-fit-content; \" class=\"wp-block-ub-table-of-contents-block ub_table-of-contents ub_table-of-contents-collapsed\" id=\"ub_table-of-contents-ebdffbe4-c583-4ec5-9fbb-75180144fbf8\" data-linktodivider=\"false\" data-showtext=\"show\" data-hidetext=\"hide\" data-scrolltype=\"auto\" data-enablesmoothscroll=\"true\" data-initiallyhideonmobile=\"false\" data-initiallyshow=\"false\"><div class=\"ub_table-of-contents-header-container\" style=\"\">\n\t\t\t<div class=\"ub_table-of-contents-header\" style=\"text-align: left; \">\n\t\t\t\t<div class=\"ub_table-of-contents-title\">Content:<\/div>\n\t\t\t\t<div class=\"ub_table-of-contents-header-toggle\">\n\t\t\t<div class=\"ub_table-of-contents-toggle\" style=\"\">\n\t\t\t\u00a0[<a class=\"ub_table-of-contents-toggle-link\" href=\"#\" style=\"\">show<\/a>]\n\t\t\t<\/div>\n\t\t<\/div>\n\t\t\t<\/div>\n\t\t<\/div><div class=\"ub_table-of-contents-extra-container\" style=\"\">\n\t\t\t<div class=\"ub_table-of-contents-container ub_table-of-contents-1-column ub-hide\">\n\t\t\t\t<ul style=\"\"><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/breadth-first-search-in-binary-trees\/#0-why-level-order-traversal\" style=\"\">Why Level-Order Traversal?<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/breadth-first-search-in-binary-trees\/#1-intuition-amp-approach\" style=\"\">Intuition &amp; Approach<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/breadth-first-search-in-binary-trees\/#2-code-implementation\" style=\"\">Code Implementation<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/breadth-first-search-in-binary-trees\/#3-code-explanation\" style=\"\">Code Explanation<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/breadth-first-search-in-binary-trees\/#4-dry-run-example\" style=\"\">Dry Run (Example)<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/breadth-first-search-in-binary-trees\/#5-time-and-space-complexity\" style=\"\">Time and Space Complexity<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/breadth-first-search-in-binary-trees\/#6-conclusion\" style=\"\">Conclusion<\/a><\/li><\/ul>\n\t\t\t<\/div>\n\t\t<\/div><\/div>\n\n\n<h2 class=\"wp-block-heading\" id=\"0-why-level-order-traversal\">Why Level-Order Traversal?<\/h2>\n\n\n\n<p>If <strong>Depth-First Search (DFS)<\/strong> is like diving deep down one path of a maze, <strong>Breadth-First Search (BFS)<\/strong> is like exploring each floor of a building one level at a time. In a <strong>binary tree<\/strong>, BFS lets us visit all nodes at depth 0 (the root), then depth 1 (its children), then depth 2, and so on.<\/p>\n\n\n\n<p>Level-order traversal (BFS) is the go-to technique for:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Finding the <strong>shortest path<\/strong> in unweighted graphs or trees.<\/li>\n\n\n\n<li>Printing a tree <strong>level by level<\/strong> (nice for debugging!).<\/li>\n\n\n\n<li>Solving problems like \u201c<strong>Zigzag Level Order<\/strong>\u201d or \u201c<strong>Average of Levels<\/strong>\u201d on LeetCode.<\/li>\n<\/ul>\n\n\n\n<p>We\u2019ll use Python\u2019s <strong><code>collections.deque<\/code><\/strong> as our queue (fast appends\/pops) and build a <code>result<\/code> list of lists, one sublist per level.<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p><em>Also read about the Python Program to <strong><a href=\"https:\/\/codeanddebug.in\/blog\/depth-first-search-in-binary-trees\/\" target=\"_blank\" rel=\"noreferrer noopener\"><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-purple-color\"><span style=\"text-decoration: underline;\">Find Maximum Depth of a Binary Tree<\/span><\/mark><\/a><\/strong><\/em>.<\/p>\n<\/blockquote>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"1-intuition-amp-approach\">Intuition &amp; Approach<\/h2>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Start at the root.<\/strong><\/li>\n\n\n\n<li><strong>Use a queue<\/strong> to hold nodes of the current level.<\/li>\n\n\n\n<li><strong>While the queue isn\u2019t empty<\/strong>:\n<ul class=\"wp-block-list\">\n<li>Note the <strong>current queue size<\/strong> (<code>level_size<\/code>), that\u2019s how many nodes are on this level.<\/li>\n\n\n\n<li><strong>Dequeue<\/strong> each node, record its value.<\/li>\n\n\n\n<li><strong>Enqueue<\/strong> its children (left then right) for the next level.<\/li>\n\n\n\n<li>After processing <code>level_size<\/code> nodes, you\u2019ve finished one level\u2014append the collected values to <code>result<\/code>.<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n\n\n\n<p>This approach ensures we visit nodes in perfect left-to-right, level-by-level order.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"2-code-implementation\">Code Implementation<\/h2>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#1E1E1E\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" data-code=\"from collections import deque\n\ndef level_order_traversal(root):\n    &quot;&quot;&quot;\n    Perform a BFS (level-order) traversal on a binary tree and\n    return a list of levels, where each level is a list of node values.\n    &quot;&quot;&quot;\n    result = []\n    if not root:\n        return result\n\n    queue = deque([root])\n\n    while queue:\n        level_size = len(queue)\n        current_level = []\n\n        for _ in range(level_size):\n            node = queue.popleft()\n            current_level.append(node.val)\n            if node.left:\n                queue.append(node.left)\n            if node.right:\n                queue.append(node.right)\n\n        result.append(current_level)\n\n    return result\" style=\"color:#D4D4D4;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki dark-plus\" style=\"background-color: #1E1E1E\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #C586C0\">from<\/span><span style=\"color: #D4D4D4\"> collections <\/span><span style=\"color: #C586C0\">import<\/span><span style=\"color: #D4D4D4\"> deque<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #569CD6\">def<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #DCDCAA\">level_order_traversal<\/span><span style=\"color: #D4D4D4\">(<\/span><span style=\"color: #9CDCFE\">root<\/span><span style=\"color: #D4D4D4\">):<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    <\/span><span style=\"color: #CE9178\">&quot;&quot;&quot;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #CE9178\">    Perform a BFS (level-order) traversal on a binary tree and<\/span><\/span>\n<span class=\"line\"><span style=\"color: #CE9178\">    return a list of levels, where each level is a list of node values.<\/span><\/span>\n<span class=\"line\"><span style=\"color: #CE9178\">    &quot;&quot;&quot;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    result = []<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    <\/span><span style=\"color: #C586C0\">if<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #569CD6\">not<\/span><span style=\"color: #D4D4D4\"> root:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #C586C0\">return<\/span><span style=\"color: #D4D4D4\"> result<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    queue = deque([root])<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    <\/span><span style=\"color: #C586C0\">while<\/span><span style=\"color: #D4D4D4\"> queue:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        level_size = <\/span><span style=\"color: #DCDCAA\">len<\/span><span style=\"color: #D4D4D4\">(queue)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        current_level = []<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #C586C0\">for<\/span><span style=\"color: #D4D4D4\"> _ <\/span><span style=\"color: #C586C0\">in<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #DCDCAA\">range<\/span><span style=\"color: #D4D4D4\">(level_size):<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">            node = queue.popleft()<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">            current_level.append(node.val)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">            <\/span><span style=\"color: #C586C0\">if<\/span><span style=\"color: #D4D4D4\"> node.left:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">                queue.append(node.left)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">            <\/span><span style=\"color: #C586C0\">if<\/span><span style=\"color: #D4D4D4\"> node.right:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">                queue.append(node.right)<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        result.append(current_level)<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    <\/span><span style=\"color: #C586C0\">return<\/span><span style=\"color: #D4D4D4\"> result<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"3-code-explanation\">Code Explanation<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Initialize:<\/strong>\n<ul class=\"wp-block-list\">\n<li><code>result = []<\/code> will hold our final list of levels.<\/li>\n\n\n\n<li>If <code>root<\/code> is <code>None<\/code>, return <code>[]<\/code> immediately.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Setup Queue:<\/strong>\n<ul class=\"wp-block-list\">\n<li><code>queue = deque([root])<\/code> seeds the BFS with the root node.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Main Loop:<\/strong>\n<ul class=\"wp-block-list\">\n<li>While <code>queue<\/code> has nodes, note <code>level_size = len(queue)<\/code>.<\/li>\n\n\n\n<li>Create an empty <code>current_level = []<\/code> to collect this level\u2019s values.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Process One Level:<\/strong>\n<ul class=\"wp-block-list\">\n<li>Loop <code>level_size<\/code> times:\n<ol class=\"wp-block-list\">\n<li><code>node = queue.popleft()<\/code> dequeues the next node.<\/li>\n\n\n\n<li><code>current_level.append(node.val)<\/code> records its value.<\/li>\n\n\n\n<li>If <code>node.left<\/code> exists, <code>queue.append(node.left)<\/code>.<\/li>\n\n\n\n<li>If <code>node.right<\/code> exists, <code>queue.append(node.right)<\/code>.<\/li>\n<\/ol>\n<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Save Level:<\/strong>\n<ul class=\"wp-block-list\">\n<li>After the for-loop, <code>current_level<\/code> holds all values for one depth\u2014append it to <code>result<\/code>.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Continue:<\/strong>\n<ul class=\"wp-block-list\">\n<li>The next iteration processes the next depth, until the queue is empty.<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"4-dry-run-example\">Dry Run (Example)<\/h2>\n\n\n\n<p>Given the tree:<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#1E1E1E\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" data-code=\"      1\n     \/ \\\n    2   3\n   \/ \\   \\\n  4   5   6\" style=\"color:#D4D4D4;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki dark-plus\" style=\"background-color: #1E1E1E\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #D4D4D4\">      1<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">     \/ \\<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    2   3<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">   \/ \\   \\<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">  4   5   6<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Start:<\/strong> <code>queue = [1]<\/code><\/li>\n\n\n\n<li><strong>Level 0:<\/strong>\n<ul class=\"wp-block-list\">\n<li><code>level_size = 1<\/code>.<\/li>\n\n\n\n<li>Dequeue <code>1<\/code> \u2192 <code>current_level = [1]<\/code>.<\/li>\n\n\n\n<li>Enqueue <code>2, 3<\/code>.<\/li>\n\n\n\n<li>Append <code>[1]<\/code> to <code>result<\/code>.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Level 1:<\/strong>\n<ul class=\"wp-block-list\">\n<li><code>queue = [2, 3]<\/code>, <code>level_size = 2<\/code>.<\/li>\n\n\n\n<li>Dequeue <code>2<\/code> \u2192 <code>[2]<\/code>, enqueue <code>4,5<\/code>.<\/li>\n\n\n\n<li>Dequeue <code>3<\/code> \u2192 <code>[2,3]<\/code>, enqueue <code>6<\/code>.<\/li>\n\n\n\n<li>Append <code>[2,3]<\/code>.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Level 2:<\/strong>\n<ul class=\"wp-block-list\">\n<li><code>queue = [4,5,6]<\/code>, <code>level_size = 3<\/code>.<\/li>\n\n\n\n<li>Dequeue <code>4<\/code> \u2192 <code>[4]<\/code>. No children.<\/li>\n\n\n\n<li>Dequeue <code>5<\/code> \u2192 <code>[4,5]<\/code>. No children.<\/li>\n\n\n\n<li>Dequeue <code>6<\/code> \u2192 <code>[4,5,6]<\/code>. No children.<\/li>\n\n\n\n<li>Append <code>[4,5,6]<\/code>.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Done:<\/strong> <code>queue<\/code> empty.<\/li>\n<\/ul>\n\n\n\n<p><strong>Final <code>result<\/code>:<\/strong> <code>[[1], [2,3], [4,5,6]]<\/code><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"5-time-and-space-complexity\">Time and Space Complexity<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Time Complexity:<\/strong>\n<ul class=\"wp-block-list\">\n<li>We visit each of the <code>N<\/code> nodes exactly once \u2192 <strong>O(N)<\/strong>.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Space Complexity:<\/strong>\n<ul class=\"wp-block-list\">\n<li>The queue may hold up to the entire width of the tree (worst case, <code>N\/2<\/code> nodes at the lowest level) \u2192 <strong>O(N)<\/strong>.<\/li>\n\n\n\n<li>The <code>result<\/code> list stores all values \u2192 <strong>O(N)<\/strong>.<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"6-conclusion\">Conclusion<\/h2>\n\n\n\n<p>Level-order traversal via BFS is <strong>incredibly powerful<\/strong> for problems that need to process nodes by depth. Python\u2019s <code>deque<\/code> keeps it elegant and efficient. Whether you\u2019re printing a tree level by level, finding the shortest path, or prepping for LeetCode\u2019s \u201cBinary Tree Zigzag\u201d question, mastering this pattern is a must-have in your toolkit.<\/p>\n\n\n\n<div class=\"wp-block-buttons is-content-justification-center is-layout-flex wp-container-core-buttons-is-layout-16018d1d wp-block-buttons-is-layout-flex\">\n<div class=\"wp-block-button\"><a class=\"wp-block-button__link wp-element-button\" href=\"https:\/\/codeanddebug.in\/course\/zero-to-hero-python-dsa\" target=\"_blank\" rel=\"noreferrer noopener\">Master DSA with our course<\/a><\/div>\n<\/div>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p><em>For any changes to the document, kindly email at <a href=\"mailto:code@codeanddebug.in\">code@codeanddebug.in<\/a> or contact us at <a href=\"tel:+91-9712928220\">+91-9712928220<\/a>.<\/em><\/p>\n<\/blockquote>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Learn how to perform a level-order (BFS) traversal on a binary tree using Python\u2019s collections.deque. This beginner-friendly guide shows you step-by-step code, intuition, and real examples, perfect for LeetCode prep! Why Level-Order Traversal? If Depth-First Search (DFS) is like diving deep down one path of a maze, Breadth-First Search (BFS) is like exploring each floor<\/p>\n","protected":false},"author":1,"featured_media":98,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3,6],"tags":[14],"class_list":{"0":"post-94","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"has-post-thumbnail","7":"category-data-structures-and-algorithm","8":"category-intermediate","9":"tag-binary-trees"},"featured_image_src":"https:\/\/codeanddebug.in\/blog\/wp-content\/uploads\/2025\/05\/breadth-first-search-featured-image.png","author_info":{"display_name":"codeanddebug","author_link":"https:\/\/codeanddebug.in\/blog\/author\/codeanddebug\/"},"_links":{"self":[{"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/posts\/94","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/comments?post=94"}],"version-history":[{"count":4,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/posts\/94\/revisions"}],"predecessor-version":[{"id":130,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/posts\/94\/revisions\/130"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/media\/98"}],"wp:attachment":[{"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/media?parent=94"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/categories?post=94"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/tags?post=94"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}