C# LeetCode 104: Maximum Depth of Binary Tree - Easy
Problem Given the root of a binary tree, return its maximum depth. The maximum depth is the number of nodes along the longest path from the root down to the farthest leaf.
General approach Recursion is used to explore all paths from the root to the leaves. Each recursive call calculates the maximum depth of the current subtree and the function combines those results to obtain the global depth.
Base case If the current node is null we have passed a leaf and the depth is 0. Example condition in code: if (root == null) return 0;
Recursive step For any non-null node the maximum depth is 1 for the node itself plus the greater of the maximum depths of the left and right subtrees. In code: return 1 + Math.Max(MaxDepth(root.left), MaxDepth(root.right));
Explanation with example Consider a simple tree with root 1, children 2 and 3, and 2 with children 4 and 5. We calculate recursively: MaxDepth(1) = 1 + max(MaxDepth(2), MaxDepth(3)). MaxDepth(2) = 1 + max(MaxDepth(4), MaxDepth(5)). MaxDepth(4) and MaxDepth(5) return 1 because their children are null. Thus MaxDepth(2) = 2, MaxDepth(3) = 1 and finally MaxDepth(1) = 3.
How the call stack works Each recursive call is stacked until reaching null. When returning from the call, 1 is added for each node to reflect the inclusion of that node in the depth. The stack ensures that a subtree is completed before combining results and returning to the upper level.
Why the numbers increase When unwinding the call stack, 1 is added on each return to count the current node, so the depth accumulates from the leaves toward the root.
Example implementation in C# public int MaxDepth(TreeNode root) { if (root == null) return 0; return 1 + Math.Max(MaxDepth(root.left), MaxDepth(root.right)); }
Complexity Time O(n) because each node is visited once. Space O(h) due to the depth of the recursion stack where h is the height of the tree; in the worst case h = n for a degenerate tree.
Practical application and benefits This recursive pattern is useful in many tree problems such as height searching, balance checking, and path calculation. Understanding the recursive call and stack management helps design efficient and correct solutions.
About Q2BSTUDIO Q2BSTUDIO is a custom software and application development company specialized in advanced technological solutions. We offer custom software, custom applications and comprehensive services in artificial intelligence, including AI for businesses, AI agents and customized automation solutions. We also provide cybersecurity services to protect your systems and data, AWS and Azure cloud services to deploy and scale applications, and business intelligence services with Power BI to turn data into actionable decisions. Our team combines experience in development, artificial intelligence and cybersecurity to deliver robust and results-oriented projects.
Keywords custom applications, custom software, artificial intelligence, cybersecurity, AWS and Azure cloud services, business intelligence services, AI for businesses, AI agents, Power BI.
Contact If you are looking to implement efficient algorithms, develop custom applications, integrate artificial intelligence or improve security and governance in the cloud, Q2BSTUDIO can help you design and deploy the right solution for your business.





