{"id":4438,"date":"2018-12-15T01:28:58","date_gmt":"2018-12-15T09:28:58","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=4438"},"modified":"2018-12-15T01:30:07","modified_gmt":"2018-12-15T09:30:07","slug":"leetcode-104-maximum-depth-of-binary-tree","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/tree\/leetcode-104-maximum-depth-of-binary-tree\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 104. Maximum Depth of Binary Tree"},"content":{"rendered":"<h1><strong>Problem<\/strong><\/h1>\n<p>Given a binary tree, find its maximum depth.<\/p>\n<p>The maximum depth is the number of nodes along the longest path from the root node down to the farthest 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 depth = 3.<\/p>\n<h1><strong>Solution: Recursion<\/strong><\/h1>\n<p>maxDepth(root) = max(maxDepth(root.left), maxDepth(root.right)) + 1<\/p>\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 maxDepth(TreeNode* root) {\r\n    if (!root) return 0;\r\n    int l = maxDepth(root-&gt;left);\r\n    int r = maxDepth(root-&gt;right);\r\n    return max(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 \"># Author: Huahua, 80 ms\r\nclass Solution:\r\n  def maxDepth(self, root):\r\n    if not root: return 0\r\n    l = self.maxDepth(root.left)\r\n    r = self.maxDepth(root.right)\r\n    return max(l, r) + 1<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Problem Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the longest 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":[222,161,28],"class_list":["post-4438","post","type-post","status-publish","format-standard","hentry","category-tree","tag-easy","tag-recusion","tag-tree","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/4438","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=4438"}],"version-history":[{"count":3,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/4438\/revisions"}],"predecessor-version":[{"id":4441,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/4438\/revisions\/4441"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=4438"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=4438"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=4438"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}