{"id":4442,"date":"2018-12-15T08:34:32","date_gmt":"2018-12-15T16:34:32","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=4442"},"modified":"2018-12-15T08:41:07","modified_gmt":"2018-12-15T16:41:07","slug":"leetcode-111-minimum-depth-of-binary-tree","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/tree\/leetcode-111-minimum-depth-of-binary-tree\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 111. Minimum Depth of Binary Tree"},"content":{"rendered":"<h1><strong>Problem<\/strong><\/h1>\n<p>Given a binary tree, find its minimum depth.<\/p>\n<p>The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node.<\/p>\n<p><strong>Note:<\/strong>\u00a0A leaf is a node with no children.<\/p>\n<p><strong>Example:<\/strong><\/p>\n<p>Given binary tree\u00a0<code>[3,9,20,null,null,15,7]<\/code>,<\/p>\n<pre class=\"crayon:false\">    3\r\n   \/ \\\r\n  9  20\r\n    \/  \\\r\n   15   7<\/pre>\n<p>return its minimum\u00a0depth = 2.<\/p>\n<h1><strong>Solution: Recursion<\/strong><\/h1>\n<p>Time complexity: O(n)<\/p>\n<p>Space complexity: O(n)<\/p>\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre class=\"lang:c++ decode:true\">\/\/ Author: Huahua, 4 ms\r\nclass Solution {\r\npublic:\r\n  int minDepth(TreeNode* root) {\r\n    if (!root) return 0;\r\n    if (!root-&gt;left &amp;&amp; !root-&gt;right) return 1;\r\n    int l = root-&gt;left ? minDepth(root-&gt;left) : INT_MAX;\r\n    int r = root-&gt;right ? minDepth(root-&gt;right) : INT_MAX;\r\n    return min(l, r) + 1;\r\n  }\r\n};<\/pre>\n\n<\/div><h2 class=\"tabtitle\">Python3<\/h2>\n<div class=\"tabcontent\">\n\n<pre class=\"lang:python decode:true \">class Solution:\r\n  def minDepth(self, root):\r\n    if not root: return 0\r\n    if not root.left and not root.right: return 1\r\n    l = self.minDepth(root.left)\r\n    r = self.minDepth(root.right)\r\n    if not root.left: return 1 + r\r\n    if not root.right: return 1 + l\r\n    return 1 + min(l, r)<\/pre>\n<\/div><\/div>\n<h1><strong>Related Problem<\/strong><\/h1>\n<ul>\n<li><a href=\"https:\/\/zxi.mytechroad.com\/blog\/tree\/leetcode-104-maximum-depth-of-binary-tree\/\">\u82b1\u82b1\u9171 LeetCode 104. Maximum Depth of Binary Tree<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Problem Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the shortest path from the root node&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[45],"tags":[319,222,28],"class_list":["post-4442","post","type-post","status-publish","format-standard","hentry","category-tree","tag-depth","tag-easy","tag-tree","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/4442","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/comments?post=4442"}],"version-history":[{"count":5,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/4442\/revisions"}],"predecessor-version":[{"id":4447,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/4442\/revisions\/4447"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=4442"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=4442"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=4442"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}