{"id":3122,"date":"2018-07-14T07:31:30","date_gmt":"2018-07-14T14:31:30","guid":{"rendered":"http:\/\/zxi.mytechroad.com\/blog\/?p=3122"},"modified":"2018-07-14T07:38:17","modified_gmt":"2018-07-14T14:38:17","slug":"leetcode-559-maximum-depth-of-n-ary-tree","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/tree\/leetcode-559-maximum-depth-of-n-ary-tree\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 559. Maximum Depth of N-ary Tree"},"content":{"rendered":"<h1>Problem<\/h1>\n<p>Given a n-ary 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>For example, given a\u00a0<code>3-ary<\/code>\u00a0tree:<\/p>\n<p>&nbsp;<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/leetcode.com\/static\/images\/problemset\/NaryTreeExample.png\" width=\"40%\" height=\"40%\" \/><\/p>\n<p>&nbsp;<\/p>\n<p>We should return its max depth, which is 3.<\/p>\n<p><b>Note:<\/b><\/p>\n<ol>\n<li>The depth of the tree is at most\u00a0<code>1000<\/code>.<\/li>\n<li>The total number of nodes is at most\u00a0<code>5000<\/code>.<\/li>\n<\/ol>\n<h1><strong>Solution: Recursion<\/strong><\/h1>\n<p>Time complexity: O(n)<\/p>\n<p>Space complexity: O(n)<\/p>\n<pre class=\"lang:default decode:true \">\/\/ Author: Huahua\r\n\/\/ Running time: 40 ms\r\nclass Solution {\r\npublic:\r\n  int maxDepth(Node* root) {\r\n    if (root == nullptr) return 0;\r\n    int depth = 0;\r\n    for (auto child : root-&gt;children)\r\n      depth = max(depth, maxDepth(child));\r\n    return depth + 1;\r\n  }\r\n};<\/pre>\n<h1><strong>Related Problems<\/strong><\/h1>\n<ul>\n<li><a href=\"http:\/\/zxi.mytechroad.com\/blog\/tree\/leetcode-590-n-ary-tree-postorder-traversal\/\">\u82b1\u82b1\u9171 LeetCode 590. N-ary Tree Postorder Traversal<\/a><\/li>\n<li><a href=\"http:\/\/zxi.mytechroad.com\/blog\/tree\/leetcode-589-n-ary-tree-preorder-traversal\/\">\u82b1\u82b1\u9171 LeetCode 589. N-ary Tree Preorder Traversal<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Problem Given a n-ary 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":[319,222,325,28],"class_list":["post-3122","post","type-post","status-publish","format-standard","hentry","category-tree","tag-depth","tag-easy","tag-n-ary","tag-tree","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/3122","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=3122"}],"version-history":[{"count":4,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/3122\/revisions"}],"predecessor-version":[{"id":3126,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/3122\/revisions\/3126"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=3122"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=3122"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=3122"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}