{"id":8743,"date":"2021-11-20T23:38:06","date_gmt":"2021-11-21T07:38:06","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=8743"},"modified":"2021-11-20T23:39:00","modified_gmt":"2021-11-21T07:39:00","slug":"leetcode-2003-smallest-missing-genetic-value-in-each-subtree","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/tree\/leetcode-2003-smallest-missing-genetic-value-in-each-subtree\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 2003. Smallest Missing Genetic Value in Each Subtree"},"content":{"rendered":"\n<p>There is a&nbsp;<strong>family tree<\/strong>&nbsp;rooted at&nbsp;<code>0<\/code>&nbsp;consisting of&nbsp;<code>n<\/code>&nbsp;nodes numbered&nbsp;<code>0<\/code>&nbsp;to&nbsp;<code>n - 1<\/code>. You are given a&nbsp;<strong>0-indexed<\/strong>&nbsp;integer array&nbsp;<code>parents<\/code>, where&nbsp;<code>parents[i]<\/code>&nbsp;is the parent for node&nbsp;<code>i<\/code>. Since node&nbsp;<code>0<\/code>&nbsp;is the&nbsp;<strong>root<\/strong>,&nbsp;<code>parents[0] == -1<\/code>.<\/p>\n\n\n\n<p>There are&nbsp;<code>10<sup>5<\/sup><\/code>&nbsp;genetic values, each represented by an integer in the&nbsp;<strong>inclusive<\/strong>&nbsp;range&nbsp;<code>[1, 10<sup>5<\/sup>]<\/code>. You are given a&nbsp;<strong>0-indexed<\/strong>&nbsp;integer array&nbsp;<code>nums<\/code>, where&nbsp;<code>nums[i]<\/code>&nbsp;is a&nbsp;<strong>distinct&nbsp;<\/strong>genetic value for node&nbsp;<code>i<\/code>.<\/p>\n\n\n\n<p>Return&nbsp;<em>an array&nbsp;<\/em><code>ans<\/code><em>&nbsp;of length&nbsp;<\/em><code>n<\/code><em>&nbsp;where&nbsp;<\/em><code>ans[i]<\/code><em>&nbsp;is<\/em>&nbsp;<em>the&nbsp;<strong>smallest<\/strong>&nbsp;genetic value that is&nbsp;<strong>missing<\/strong>&nbsp;from the subtree rooted at node<\/em>&nbsp;<code>i<\/code>.<\/p>\n\n\n\n<p>The&nbsp;<strong>subtree<\/strong>&nbsp;rooted at a node&nbsp;<code>x<\/code>&nbsp;contains node&nbsp;<code>x<\/code>&nbsp;and all of its&nbsp;<strong>descendant<\/strong>&nbsp;nodes.<\/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\/2021\/08\/23\/case-1.png\" alt=\"\"\/><\/figure>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> parents = [-1,0,0,2], nums = [1,2,3,4]\n<strong>Output:<\/strong> [5,1,1,1]\n<strong>Explanation:<\/strong> The answer for each subtree is calculated as follows:\n- 0: The subtree contains nodes [0,1,2,3] with values [1,2,3,4]. 5 is the smallest missing value.\n- 1: The subtree contains only node 1 with value 2. 1 is the smallest missing value.\n- 2: The subtree contains nodes [2,3] with values [3,4]. 1 is the smallest missing value.\n- 3: The subtree contains only node 3 with value 4. 1 is the smallest missing value.\n<\/pre>\n\n\n\n<p><strong>Example 2:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/assets.leetcode.com\/uploads\/2021\/08\/23\/case-2.png\" alt=\"\"\/><\/figure>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> parents = [-1,0,1,0,3,3], nums = [5,4,6,2,1,3]\n<strong>Output:<\/strong> [7,1,1,4,2,1]\n<strong>Explanation:<\/strong> The answer for each subtree is calculated as follows:\n- 0: The subtree contains nodes [0,1,2,3,4,5] with values [5,4,6,2,1,3]. 7 is the smallest missing value.\n- 1: The subtree contains nodes [1,2] with values [4,6]. 1 is the smallest missing value.\n- 2: The subtree contains only node 2 with value 6. 1 is the smallest missing value.\n- 3: The subtree contains nodes [3,4,5] with values [2,1,3]. 4 is the smallest missing value.\n- 4: The subtree contains only node 4 with value 1. 2 is the smallest missing value.\n- 5: The subtree contains only node 5 with value 3. 1 is the smallest missing value.\n<\/pre>\n\n\n\n<p><strong>Example 3:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> parents = [-1,2,3,0,2,4,1], nums = [2,3,4,5,6,7,8]\n<strong>Output:<\/strong> [1,1,1,1,1,1,1]\n<strong>Explanation:<\/strong> The value 1 is missing from all the subtrees.\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>n == parents.length == nums.length<\/code><\/li><li><code>2 &lt;= n &lt;= 10<sup>5<\/sup><\/code><\/li><li><code>0 &lt;= parents[i] &lt;= n - 1<\/code>&nbsp;for&nbsp;<code>i != 0<\/code><\/li><li><code>parents[0] == -1<\/code><\/li><li><code>parents<\/code>&nbsp;represents a valid tree.<\/li><li><code>1 &lt;= nums[i] &lt;= 10<sup>5<\/sup><\/code><\/li><li>Each&nbsp;<code>nums[i]<\/code>&nbsp;is distinct.<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: DFS on a single path<\/strong><\/h2>\n\n\n\n<p>One ancestors of node with value of 1 will have missing values greater than 1. We do a dfs on the path that from node with value 1 to the root.<\/p>\n\n\n\n<p>Time complexity: O(n + max(nums))<br>Space complexity: O(n + max(nums)) <\/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  vector<int> smallestMissingValueSubtree(vector<int>& parents, vector<int>& nums) {\n    const int n = parents.size();\n    vector<int> ans(n, 1);\n    vector<int> seen(100002);\n    vector<vector<int>> g(n);\n    for (int i = 1; i < n; ++i)\n      g[parents[i]].push_back(i);\n    function<void(int)> dfs = [&](int u) {\n      if (seen[nums[u]]++) return;\n      for (int v : g[u]) dfs(v);\n    };\n    int u = find(begin(nums), end(nums), 1) - begin(nums);\n    for (int l = 1; u < n &#038;&#038; u != -1; u = parents[u]) {\n      dfs(u);\n      while (seen[l]) ++l;\n      ans[u] = l;\n    }\n    return ans;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>There is a&nbsp;family tree&nbsp;rooted at&nbsp;0&nbsp;consisting of&nbsp;n&nbsp;nodes numbered&nbsp;0&nbsp;to&nbsp;n &#8211; 1. You are given a&nbsp;0-indexed&nbsp;integer array&nbsp;parents, where&nbsp;parents[i]&nbsp;is the parent for node&nbsp;i. Since node&nbsp;0&nbsp;is the&nbsp;root,&nbsp;parents[0] == -1. There&#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":[33,217,28],"class_list":["post-8743","post","type-post","status-publish","format-standard","hentry","category-tree","tag-dfs","tag-hard","tag-tree","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8743","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=8743"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8743\/revisions"}],"predecessor-version":[{"id":8745,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8743\/revisions\/8745"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=8743"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=8743"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=8743"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}