{"id":120,"date":"2025-05-14T12:11:15","date_gmt":"2025-05-14T06:41:15","guid":{"rendered":"https:\/\/codeanddebug.in\/blog\/?p=120"},"modified":"2025-05-14T12:29:34","modified_gmt":"2025-05-14T06:59:34","slug":"maximum-depth-of-binary-tree","status":"publish","type":"post","link":"https:\/\/codeanddebug.in\/blog\/maximum-depth-of-binary-tree\/","title":{"rendered":"Maximum Depth of Binary Tree | Explained with Examples"},"content":{"rendered":"\n<p>How to calculate the<strong> maximum depth<\/strong> of binary tree with simple Python recursion. This beginner-friendly article breaks down the \u201cwhy\u201d and \u201chow,\u201d complete with code, walkthrough, and complexity analysis. You can solve the question from this Leetcode 104 &#8211; [<a href=\"https:\/\/leetcode.com\/problems\/maximum-depth-of-binary-tree\/description\/\" target=\"_blank\" rel=\"noreferrer noopener\"><strong><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-purple-color\"><span style=\"text-decoration: underline;\">Problem Link<\/span><\/mark><\/strong><\/a>].<\/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-24625ae0-1e35-4eb2-920d-1a597fd361e4\" 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\/maximum-depth-of-binary-tree\/#0-why-tree-height-matters\" style=\"\">Why Tree Height Matters<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/maximum-depth-of-binary-tree\/#1-recursive-solution\" style=\"\">Recursive Solution<\/a><ul><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/maximum-depth-of-binary-tree\/#2-1-intuition-amp-approach\" style=\"\">1. Intuition &amp; Approach<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/maximum-depth-of-binary-tree\/#3-2-code-implementation\" style=\"\">2. Code Implementation<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/maximum-depth-of-binary-tree\/#4-3-code-explanation\" style=\"\">3. Code Explanation<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/maximum-depth-of-binary-tree\/#5-4-dry-run\" style=\"\">4. Dry Run<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/maximum-depth-of-binary-tree\/#6-5-time-amp-space-complexity\" style=\"\">5. Time &amp; Space Complexity<\/a><\/li><\/ul><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/maximum-depth-of-binary-tree\/#7-iterative-solution\" style=\"\">Iterative Solution<\/a><ul><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/maximum-depth-of-binary-tree\/#8-1-intuition-amp-approach\" style=\"\">1. Intuition &amp; Approach<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/maximum-depth-of-binary-tree\/#9-2-code-implementation\" style=\"\">2. Code Implementation<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/maximum-depth-of-binary-tree\/#10-3-dry-run-example\" style=\"\">3. Dry Run Example<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/maximum-depth-of-binary-tree\/#11-4-time-amp-space-complexity\" style=\"\">4. Time &amp; Space Complexity<\/a><\/li><\/ul><\/li><\/ul>\n\t\t\t<\/div>\n\t\t<\/div><\/div>\n\n\n<h2 class=\"wp-block-heading\" id=\"0-why-tree-height-matters\">Why Tree Height Matters<\/h2>\n\n\n\n<p>The <strong>height<\/strong> of a binary tree is the number of nodes along the longest path from the root down to a leaf. In algorithmic terms, it\u2019s also called the <strong>maximum depth<\/strong>. Knowing the height is fundamental because:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>It tells you how balanced (or skewed) your tree is.<\/li>\n\n\n\n<li>Many operations (insert, delete, search) depends on tree height for efficiency.<\/li>\n<\/ul>\n\n\n\n<p>We\u2019ll use <strong>simple recursion<\/strong> and Python. By the end, you\u2019ll see how a few lines of code can measure your tree\u2019s height in <strong>O(N)<\/strong> time.<\/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 Program to <strong><a href=\"https:\/\/codeanddebug.in\/blog\/diameter-of-binary-tree\/\" 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 Diameter of a Binary Tree<\/span><\/mark><\/a><\/strong><\/em>.<\/p>\n<\/blockquote>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"1-recursive-solution\">Recursive Solution<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"2-1-intuition-amp-approach\">1. Intuition &amp; Approach<\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Base Case (Empty Tree):<\/strong>\n<ul class=\"wp-block-list\">\n<li>If a node is <code>None<\/code>, its height is <code>0<\/code>.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Recursive Case:<\/strong>\n<ul class=\"wp-block-list\">\n<li>Compute the height of the <strong>left subtree<\/strong>.<\/li>\n\n\n\n<li>Compute the height of the <strong>right subtree<\/strong>.<\/li>\n\n\n\n<li>The height at the current node is <code>1 + max(leftHeight, rightHeight)<\/code>.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Why It Works:<\/strong>\n<ul class=\"wp-block-list\">\n<li>You\u2019re essentially asking each subtree \u201chow tall are you?\u201d and adding one for the current node.<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"3-2-code-implementation\">2. Code Implementation<\/h3>\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=\"class Solution:\n    def solve(self, node):\n        # 1. Base case: an empty subtree has height 0\n        if node is None:\n            return 0\n        \n        # 2. Recurse on left and right\n        leftHeight  = self.solve(node.left)\n        rightHeight = self.solve(node.right)\n        \n        # 3. Current node\u2019s height = 1 + taller subtree\n        return 1 + max(leftHeight, rightHeight)\n\n    def maxDepth(self, root: Optional[TreeNode]) -&gt; int:\n        return self.solve(root)\" 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: #569CD6\">class<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #4EC9B0\">Solution<\/span><span style=\"color: #D4D4D4\">:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    <\/span><span style=\"color: #569CD6\">def<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #DCDCAA\">solve<\/span><span style=\"color: #D4D4D4\">(<\/span><span style=\"color: #9CDCFE\">self<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #9CDCFE\">node<\/span><span style=\"color: #D4D4D4\">):<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #6A9955\"># 1. Base case: an empty subtree has height 0<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #C586C0\">if<\/span><span style=\"color: #D4D4D4\"> node <\/span><span style=\"color: #569CD6\">is<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #569CD6\">None<\/span><span style=\"color: #D4D4D4\">:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">            <\/span><span style=\"color: #C586C0\">return<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #B5CEA8\">0<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #6A9955\"># 2. Recurse on left and right<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        leftHeight  = <\/span><span style=\"color: #569CD6\">self<\/span><span style=\"color: #D4D4D4\">.solve(node.left)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        rightHeight = <\/span><span style=\"color: #569CD6\">self<\/span><span style=\"color: #D4D4D4\">.solve(node.right)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #6A9955\"># 3. Current node\u2019s height = 1 + taller subtree<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #C586C0\">return<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #B5CEA8\">1<\/span><span style=\"color: #D4D4D4\"> + <\/span><span style=\"color: #DCDCAA\">max<\/span><span style=\"color: #D4D4D4\">(leftHeight, rightHeight)<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    <\/span><span style=\"color: #569CD6\">def<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #DCDCAA\">maxDepth<\/span><span style=\"color: #D4D4D4\">(<\/span><span style=\"color: #9CDCFE\">self<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #9CDCFE\">root<\/span><span style=\"color: #D4D4D4\">: Optional[TreeNode]) -&gt; <\/span><span style=\"color: #4EC9B0\">int<\/span><span style=\"color: #D4D4D4\">:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #C586C0\">return<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #569CD6\">self<\/span><span style=\"color: #D4D4D4\">.solve(root)<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"4-3-code-explanation\">3. Code Explanation<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong><code>solve(node)<\/code><\/strong>\n<ul class=\"wp-block-list\">\n<li><strong>If <code>node<\/code> is <code>None<\/code><\/strong>, return <code>0<\/code>.<\/li>\n\n\n\n<li>Otherwise,\n<ul class=\"wp-block-list\">\n<li>Recursively get <code>leftHeight<\/code> from <code>node.left<\/code>.<\/li>\n\n\n\n<li>Recursively get <code>rightHeight<\/code> from <code>node.right<\/code>.<\/li>\n\n\n\n<li>Return <code>1 + max(leftHeight, rightHeight)<\/code>.<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong><code>maxDepth(root)<\/code><\/strong> just kicks off the recursion at the tree\u2019s root.<\/li>\n<\/ul>\n\n\n\n<p>This compact recursion mirrors the definition of tree height itself\u2014a beautiful alignment of concept and code!<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"5-4-dry-run\">4. Dry Run<\/h3>\n\n\n\n<p>Given this 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\n           \\\n            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<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">           \\<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">            6<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>At node 4<\/strong> \u2192 both children <code>None<\/code> \u2192 height = <code>1 + max(0,0) = 1<\/code>.<\/li>\n\n\n\n<li><strong>At node 2<\/strong> \u2192 left=1 (from 4), right=0 \u2192 height = <code>1 + max(1,0) = 2<\/code>.<\/li>\n\n\n\n<li><strong>At node 6<\/strong> \u2192 height = 1.<\/li>\n\n\n\n<li><strong>At node 5<\/strong> \u2192 left=0, right=1 \u2192 height = 2.<\/li>\n\n\n\n<li><strong>At node 3<\/strong> \u2192 left=0, right=2 \u2192 height = 3.<\/li>\n\n\n\n<li><strong>At root (1)<\/strong> \u2192 left=2, right=3 \u2192 height = <code>1 + max(2,3) = 4<\/code>.<\/li>\n<\/ul>\n\n\n\n<p>So the tree\u2019s height is <strong>4<\/strong>.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"6-5-time-amp-space-complexity\">5. Time &amp; Space Complexity<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Time Complexity:<\/strong>\n<ul class=\"wp-block-list\">\n<li>You visit each node exactly once \u2192 <strong>O(N)<\/strong> for N nodes.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Space Complexity:<\/strong>\n<ul class=\"wp-block-list\">\n<li><strong>O(H)<\/strong> recursion call stack, where H is the tree height.<\/li>\n\n\n\n<li>In the worst (skewed) case H \u2248 N \u2192 <strong>O(N)<\/strong>; in a balanced tree H \u2248 log N.<\/li>\n<\/ul>\n<\/li>\n<\/ul>\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\/master-dsa-with-leetcode\" target=\"_blank\" rel=\"noreferrer noopener\">Join our free DSA COURSE<\/a><\/div>\n<\/div>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"7-iterative-solution\">Iterative Solution<\/h2>\n\n\n\n<p>We all know the recursive trick to find a tree\u2019s height: dive left, dive right, take the bigger, and add one. But what if you preferred an <strong>iterative<\/strong> approach, no function call stack, just a good old <strong>queue<\/strong>? That\u2019s where <strong>breadth-first search (BFS)<\/strong> or <strong>level-order traversal<\/strong> comes.<\/p>\n\n\n\n<p>By walking the tree <strong>level by level<\/strong>, you can count how many \u201cfloors\u201d it has. Each time you finish processing one level, you increase the height by one.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"8-1-intuition-amp-approach\">1. Intuition &amp; Approach<\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Use a queue to hold nodes at the current level.<\/strong><\/li>\n\n\n\n<li><strong>Initialize<\/strong> <code>height = 0<\/code>.<\/li>\n\n\n\n<li><strong>While the queue isn\u2019t empty<\/strong>:\n<ul class=\"wp-block-list\">\n<li>Note how many nodes are on this level (<code>level_size = len(queue)<\/code>).<\/li>\n\n\n\n<li><strong>Process exactly<\/strong><code>level_size<\/code> nodes:\n<ul class=\"wp-block-list\">\n<li>Dequeue a node, enqueue its children (if any).<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>After<\/strong> processing those nodes, you\u2019ve completed one level, increment <code>height<\/code>.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Stop<\/strong> when there are no more levels (queue is empty); <code>height<\/code> now holds the tree\u2019s maximum depth.<\/li>\n<\/ol>\n\n\n\n<p>This approach guarantees you visit every node exactly once\u2014<strong>O(N)<\/strong> time\u2014and your queue never holds more nodes than one full level\u2014<strong>O(W)<\/strong> space, where W is the maximum width.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"9-2-code-implementation\">2. Code Implementation<\/h3>\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\nclass Solution:\n    def maxDepth(self, root: Optional[TreeNode]) -&gt; int:\n        # If the tree is empty, its height is 0\n        if not root:\n            return 0\n\n        queue = deque([root])\n        height = 0\n\n        # Process level by level\n        while queue:\n            level_size = len(queue)\n            height += 1  # we\u2019re about to process one more level\n\n            # Dequeue all nodes in the current level, enqueue their children\n            for _ in range(level_size):\n                node = queue.popleft()\n                if node.left:\n                    queue.append(node.left)\n                if node.right:\n                    queue.append(node.right)\n\n        return height\" 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\">class<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #4EC9B0\">Solution<\/span><span style=\"color: #D4D4D4\">:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    <\/span><span style=\"color: #569CD6\">def<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #DCDCAA\">maxDepth<\/span><span style=\"color: #D4D4D4\">(<\/span><span style=\"color: #9CDCFE\">self<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #9CDCFE\">root<\/span><span style=\"color: #D4D4D4\">: Optional[TreeNode]) -&gt; <\/span><span style=\"color: #4EC9B0\">int<\/span><span style=\"color: #D4D4D4\">:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #6A9955\"># If the tree is empty, its height is 0<\/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\"> <\/span><span style=\"color: #B5CEA8\">0<\/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 style=\"color: #D4D4D4\">        height = <\/span><span style=\"color: #B5CEA8\">0<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #6A9955\"># Process level by level<\/span><\/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\">            height += <\/span><span style=\"color: #B5CEA8\">1<\/span><span style=\"color: #D4D4D4\">  <\/span><span style=\"color: #6A9955\"># we\u2019re about to process one more level<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">            <\/span><span style=\"color: #6A9955\"># Dequeue all nodes in the current level, enqueue their children<\/span><\/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\">                <\/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\">        <\/span><span style=\"color: #C586C0\">return<\/span><span style=\"color: #D4D4D4\"> height<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"10-3-dry-run-example\">3. Dry Run Example<\/h3>\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\" 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\">      <\/span><span style=\"color: #B5CEA8\">1<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">     \/ \\<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    <\/span><span style=\"color: #B5CEA8\">2<\/span><span style=\"color: #D4D4D4\">   <\/span><span style=\"color: #B5CEA8\">3<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">   \/     \\<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">  <\/span><span style=\"color: #B5CEA8\">4<\/span><span style=\"color: #D4D4D4\">       <\/span><span style=\"color: #B5CEA8\">5<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Start:<\/strong> <code>queue = [1]<\/code>, <code>height = 0<\/code><\/li>\n\n\n\n<li><strong>Loop 1:<\/strong>\n<ul class=\"wp-block-list\">\n<li><code>level_size = 1<\/code>, <code>height = 1<\/code><\/li>\n\n\n\n<li>Pop <code>1<\/code>, enqueue <code>[2, 3]<\/code><\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Loop 2:<\/strong>\n<ul class=\"wp-block-list\">\n<li><code>queue = [2, 3]<\/code>, <code>level_size = 2<\/code>, <code>height = 2<\/code><\/li>\n\n\n\n<li>Pop <code>2<\/code> \u2192 enqueue <code>[4]<\/code>; pop <code>3<\/code> \u2192 enqueue <code>[4, 5]<\/code><\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Loop 3:<\/strong>\n<ul class=\"wp-block-list\">\n<li><code>queue = [4, 5]<\/code>, <code>level_size = 2<\/code>, <code>height = 3<\/code><\/li>\n\n\n\n<li>Pop <code>4<\/code> (no children), pop <code>5<\/code> (no children) \u2192 <code>queue = []<\/code><\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>End:<\/strong> queue is empty, return <code>height = 3<\/code><\/li>\n<\/ul>\n\n\n\n<p>So the tree has <strong>3 levels<\/strong>:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>node 1, 2) nodes 2 &amp; 3, 3) nodes 4 &amp; 5.<\/li>\n<\/ol>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"11-4-time-amp-space-complexity\">4. Time &amp; Space Complexity<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Time:<\/strong> O(N) \u2014 you touch each node once.<\/li>\n\n\n\n<li><strong>Space:<\/strong> O(W) \u2014 at most one level\u2019s worth of nodes in the queue (worst-case W \u2248 N\/2).<\/li>\n<\/ul>\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\">Join our advance dsa course<\/a><\/div>\n<\/div>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p><em>For any changes to the article, 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\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>How to calculate the maximum depth of binary tree with simple Python recursion. This beginner-friendly article breaks down the \u201cwhy\u201d and \u201chow,\u201d complete with code, walkthrough, and complexity analysis. You can solve the question from this Leetcode 104 &#8211; [Problem Link]. Why Tree Height Matters The height of a binary tree is the number of<\/p>\n","protected":false},"author":1,"featured_media":121,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3,6],"tags":[14],"class_list":{"0":"post-120","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\/maximum-depth-of-a-binary-tree-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\/120","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=120"}],"version-history":[{"count":2,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/posts\/120\/revisions"}],"predecessor-version":[{"id":129,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/posts\/120\/revisions\/129"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/media\/121"}],"wp:attachment":[{"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/media?parent=120"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/categories?post=120"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/tags?post=120"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}