{"id":10188,"date":"2024-10-12T22:02:58","date_gmt":"2024-10-13T05:02:58","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=10188"},"modified":"2024-10-12T22:04:03","modified_gmt":"2024-10-13T05:04:03","slug":"leetcode-3319-k-th-largest-perfect-subtree-size-in-binary-tree","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/tree\/leetcode-3319-k-th-largest-perfect-subtree-size-in-binary-tree\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode\u00a03319. K-th Largest Perfect Subtree Size in Binary Tree"},"content":{"rendered":"\n<p>You are given the&nbsp;<code>root<\/code>&nbsp;of a&nbsp;<strong>binary tree<\/strong>&nbsp;and an integer&nbsp;<code>k<\/code>.<\/p>\n\n\n\n<p>Return an integer denoting the size of the&nbsp;<code>k<sup>th<\/sup><\/code>&nbsp;<strong>largest<em>&nbsp;<\/em>perfect binary<\/strong><em>&nbsp;<\/em><\/p>\n\n\n\n<p>subtree, or&nbsp;<code>-1<\/code>&nbsp;if it doesn&#8217;t exist.<\/p>\n\n\n\n<p>A&nbsp;<strong>perfect binary tree<\/strong>&nbsp;is a tree where all leaves are on the same level, and every parent has two children.<\/p>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n\n<p><strong>Input:<\/strong>&nbsp;root = [5,3,6,5,2,5,7,1,8,null,null,6,8], k = 2<\/p>\n\n\n\n<p><strong>Output:<\/strong>&nbsp;3<\/p>\n\n\n\n<p><strong>Explanation:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/assets.leetcode.com\/uploads\/2024\/06\/21\/image.jpg\" alt=\"\"\/><\/figure>\n\n\n\n<p>The roots of the perfect binary subtrees are highlighted in black. Their sizes, in decreasing order are&nbsp;<code>[3, 3, 1, 1, 1, 1, 1, 1]<\/code>.<br>The&nbsp;<code>2<sup>nd<\/sup><\/code>&nbsp;largest size is 3.<\/p>\n\n\n\n<p><strong>Example 2:<\/strong><\/p>\n\n\n\n<p><strong>Input:<\/strong>&nbsp;root = [1,2,3,4,5,6,7], k = 1<\/p>\n\n\n\n<p><strong>Output:<\/strong>&nbsp;7<\/p>\n\n\n\n<p><strong>Explanation:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/assets.leetcode.com\/uploads\/2024\/06\/21\/image1.jpg\" alt=\"\"\/><\/figure>\n\n\n\n<p>The sizes of the perfect binary subtrees in decreasing order are&nbsp;<code>[7, 3, 3, 1, 1, 1, 1]<\/code>. The size of the largest perfect binary subtree is 7.<\/p>\n\n\n\n<p><strong>Example 3:<\/strong><\/p>\n\n\n\n<p><strong>Input:<\/strong>&nbsp;root = [1,2,3,null,4], k = 3<\/p>\n\n\n\n<p><strong>Output:<\/strong>&nbsp;-1<\/p>\n\n\n\n<p><strong>Explanation:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/assets.leetcode.com\/uploads\/2024\/06\/21\/image4.jpg\" alt=\"\"\/><\/figure>\n\n\n\n<p>The sizes of the perfect binary subtrees in decreasing order are&nbsp;<code>[1, 1]<\/code>. There are fewer than 3 perfect binary subtrees.<\/p>\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 in the range\u00a0<code>[1, 2000]<\/code>.<\/li><li><code>1 &lt;= Node.val &lt;= 2000<\/code><\/li><li><code>1 &lt;= k &lt;= 1024<\/code><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: DFS<\/strong><\/h2>\n\n\n\n<p>Write a function f() to return the perfect subtree size at node n.<\/p>\n\n\n\n<p>def f(TreeNode n):<br>  if not n: return 0<br>  l, r = f(n.left), f(n.right)<br>  return l + r + 1 if l == r &amp;&amp; l != -1 else -1<\/p>\n\n\n\n<p>Time complexity: O(n + KlogK)<br>Space complexity: O(n)<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Solution {\npublic:\n  int kthLargestPerfectSubtree(TreeNode* root, int k) {\n    vector&lt;int> ss;\n    function&lt;int(TreeNode*)> PerfectSubtree = &#91;&amp;](TreeNode* node) -> int {\n      if (node == nullptr) return 0;\n      int l = PerfectSubtree(node->left);\n      int r = PerfectSubtree(node->right);\n      if (l == r &amp;&amp; l != -1) {\n        ss.push_back(l + r + 1);\n        return ss.back();\n      }\n      return -1;  \n    };\n    PerfectSubtree(root);\n    sort(rbegin(ss), rend(ss));\n    return k &lt;= ss.size() ? ss&#91;k - 1] : -1;\n  }\n};<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>You are given the&nbsp;root&nbsp;of a&nbsp;binary tree&nbsp;and an integer&nbsp;k. Return an integer denoting the size of the&nbsp;kth&nbsp;largest&nbsp;perfect binary&nbsp; subtree, or&nbsp;-1&nbsp;if it doesn&#8217;t exist. A&nbsp;perfect binary tree&nbsp;is&#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":[30,33,177,823,28],"class_list":["post-10188","post","type-post","status-publish","format-standard","hentry","category-tree","tag-binary-tree","tag-dfs","tag-medium","tag-perfect-tree","tag-tree","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/10188","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=10188"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/10188\/revisions"}],"predecessor-version":[{"id":10191,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/10188\/revisions\/10191"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=10188"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=10188"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=10188"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}