{"id":5897,"date":"2019-11-30T21:25:33","date_gmt":"2019-12-01T05:25:33","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=5897"},"modified":"2019-11-30T21:26:10","modified_gmt":"2019-12-01T05:26:10","slug":"leetcode-1273-delete-tree-nodes","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/tree\/leetcode-1273-delete-tree-nodes\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1273. Delete Tree Nodes"},"content":{"rendered":"\n<p>A tree rooted at node 0 is given as follows:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>The number of nodes is&nbsp;<code>nodes<\/code>;<\/li><li>The value of the&nbsp;<code>i<\/code>-th node is&nbsp;<code>value[i]<\/code>;<\/li><li>The parent of the&nbsp;<code>i<\/code>-th node is&nbsp;<code>parent[i]<\/code>.<\/li><\/ul>\n\n\n\n<p>Remove every subtree whose sum of values of nodes is zero.<\/p>\n\n\n\n<p>After doing so, return the number of nodes remaining in the tree.<\/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\/02\/1421_sample_1.PNG\" alt=\"\"\/><\/figure>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> nodes = 7, parent = [-1,0,0,1,2,2,2], value = [1,-2,4,0,-2,-1,-1]\n<strong>Output:<\/strong> 2\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>1 &lt;= nodes &lt;= 10^4<\/code><\/li><li><code>-10^5 &lt;= value[i] &lt;= 10^5<\/code><\/li><li><code>parent.length == nodes<\/code><\/li><li><code>parent[0] == -1<\/code>&nbsp;which indicates that&nbsp;<code>0<\/code>&nbsp;is the root.<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Inorder Traversal<\/strong><\/h2>\n\n\n\n<p>For each node, return the sum of all its subtrees and number of nodes including itself after removal.<\/p>\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 deleteTreeNodes(int nodes, vector<int>& parent, vector<int>& value) {\n    vector<vector<int>> g(nodes);\n    for (int i = 1; i < nodes; ++i)\n      g[parent[i]].push_back(i);    \n    \/\/ Returns <sum, nodes> of subtree n.\n    function<pair<int,int>(int)> traverse = [&](int n) {\n      int sum = value[n];\n      int count = 1;\n      for (int x : g[n]) {\n        auto [s, c] = traverse(x);        \n        sum += s;\n        count += s ? c : 0;\n      }\n      return make_pair(sum, sum ? count : 0);\n    };    \n    return traverse(0).second;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>A tree rooted at node 0 is given as follows: The number of nodes is&nbsp;nodes; The value of the&nbsp;i-th node is&nbsp;value[i]; The parent of the&nbsp;i-th&#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":[58,177,28],"class_list":["post-5897","post","type-post","status-publish","format-standard","hentry","category-tree","tag-inorder","tag-medium","tag-tree","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/5897","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=5897"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/5897\/revisions"}],"predecessor-version":[{"id":5899,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/5897\/revisions\/5899"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=5897"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=5897"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=5897"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}