{"id":6221,"date":"2020-01-31T23:39:46","date_gmt":"2020-02-01T07:39:46","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=6221"},"modified":"2020-01-31T23:40:06","modified_gmt":"2020-02-01T07:40:06","slug":"leetcode-1302-deepest-leaves-sum","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/tree\/leetcode-1302-deepest-leaves-sum\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1302. Deepest Leaves Sum"},"content":{"rendered":"\n<p>Given a binary tree, return the sum of values of its deepest leaves.<\/p>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/assets.leetcode.com\/uploads\/2019\/07\/31\/1483_ex1.png\" alt=\"\"\/><\/figure>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> root = [1,2,3,4,5,null,6,7,null,null,null,null,8]\n<strong>Output:<\/strong> 15\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>The number of nodes in the tree is between&nbsp;<code>1<\/code>&nbsp;and&nbsp;<code>10^4<\/code>.<\/li><li>The value of nodes is between&nbsp;<code>1<\/code>&nbsp;and&nbsp;<code>100<\/code>.<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution 1: Recursion<\/strong><\/h2>\n\n\n\n<p>Time complexity: O(n)<br>Space complexity: O(n)<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"C++\">\n\/\/ Author: Huahua\nclass Solution {\npublic:\n  int deepestLeavesSum(TreeNode* root) {\n    int sum = 0;\n    int max_depth = 0;\n    function<void(TreeNode*, int)> dfs = [&](TreeNode* n, int d) {\n      if (!n) return;\n      if (d > max_depth) {\n        max_depth = d;\n        sum = 0;\n      }\n      if (d == max_depth) sum += n->val;\n      dfs(n->left, d + 1);\n      dfs(n->right, d + 1);\n    };\n    dfs(root, 0);\n    return sum;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Given a binary tree, return the sum of values of its deepest leaves. Example 1: Input: root = [1,2,3,4,5,null,6,7,null,null,null,null,8] Output: 15 Constraints: The number of&#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":[177,376,28],"class_list":["post-6221","post","type-post","status-publish","format-standard","hentry","category-tree","tag-medium","tag-on","tag-tree","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6221","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=6221"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6221\/revisions"}],"predecessor-version":[{"id":6223,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6221\/revisions\/6223"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=6221"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=6221"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=6221"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}