{"id":10013,"date":"2023-04-29T08:57:33","date_gmt":"2023-04-29T15:57:33","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=10013"},"modified":"2023-04-29T12:14:19","modified_gmt":"2023-04-29T19:14:19","slug":"leetcode-2641-cousins-in-binary-tree-ii","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/uncategorized\/leetcode-2641-cousins-in-binary-tree-ii\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 2641. Cousins in Binary Tree II"},"content":{"rendered":"\n<p>Given the&nbsp;<code>root<\/code>&nbsp;of a binary tree, replace the value of each node in the tree with the&nbsp;<strong>sum of all its cousins&#8217; values<\/strong>.<\/p>\n\n\n\n<p>Two nodes of a binary tree are&nbsp;<strong>cousins<\/strong>&nbsp;if they have the same depth with different parents.<\/p>\n\n\n\n<p>Return&nbsp;<em>the&nbsp;<\/em><code>root<\/code><em>&nbsp;of the modified tree<\/em>.<\/p>\n\n\n\n<p><strong>Note<\/strong>&nbsp;that the depth of a node is the number of edges in the path from the root node to it.<\/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\/2023\/01\/11\/example11.png\" alt=\"\"\/><\/figure>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> root = [5,4,9,1,10,null,7]\n<strong>Output:<\/strong> [0,0,0,7,7,null,11]\n<strong>Explanation:<\/strong> The diagram above shows the initial binary tree and the binary tree after changing the value of each node.\n- Node with value 5 does not have any cousins so its sum is 0.\n- Node with value 4 does not have any cousins so its sum is 0.\n- Node with value 9 does not have any cousins so its sum is 0.\n- Node with value 1 has a cousin with value 7 so its sum is 7.\n- Node with value 10 has a cousin with value 7 so its sum is 7.\n- Node with value 7 has cousins with values 1 and 10 so its sum is 11.\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\/2023\/01\/11\/diagram33.png\" alt=\"\"\/><\/figure>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> root = [3,1,2]\n<strong>Output:<\/strong> [0,0,0]\n<strong>Explanation:<\/strong> The diagram above shows the initial binary tree and the binary tree after changing the value of each node.\n- Node with value 3 does not have any cousins so its sum is 0.\n- Node with value 1 does not have any cousins so its sum is 0.\n- Node with value 2 does not have any cousins so its sum is 0.\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 in the range&nbsp;<code>[1, 10<sup>5<\/sup>]<\/code>.<\/li><li><code>1 &lt;= Node.val &lt;= 10<sup>4<\/sup><\/code><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Level Order Sum<\/strong><\/h2>\n\n\n\n<p>Time complexity: O(n)<br>Space complexity: O(n)<\/p>\n\n\n\n<p>DFS, two passes<\/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  TreeNode* replaceValueInTree(TreeNode* root) {\n    unordered_map<TreeNode*, int> parentSum;\n    parentSum[nullptr] = root->val;\n    vector<int> levelSums;\n    function<void(TreeNode*, int)> dfs = [&](TreeNode* node, int depth) {\n      if (!node) return;\n      if (levelSums.size() <= depth) {\n        levelSums.push_back(0);\n      }\n      levelSums[depth] += node->val;\n      if (node->left) {\n        parentSum[node] += node->left->val;\n        dfs(node->left, depth + 1);\n      }\n      if (node->right) {\n        parentSum[node] += node->right->val;\n        dfs(node->right, depth + 1);\n      }\n    };\n    function<void(TreeNode*, TreeNode*, int)> dfs2 = \n    [&](TreeNode* node, TreeNode* p, int depth) {\n      if (!node) return;\n      node->val = levelSums[depth] - parentSum[p];\n      dfs2(node->left, node, depth + 1);\n      dfs2(node->right, node, depth + 1);\n    };\n    dfs(root, 0);\n    dfs2(root, nullptr, 0);\n    return root;\n  }\n};\n<\/pre>\n<\/div><\/div>\n\n\n\n<p>BFS, one+ pass<\/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  TreeNode* replaceValueInTree(TreeNode* root) {\n    root->val = 0;\n    queue<TreeNode*> q({root});\n    while (!q.empty()) {\n      vector<TreeNode*> nodes;\n      int sum = 0;\n      for (int s = q.size(); s; --s) {\n        TreeNode* node = q.front(); q.pop();\n        nodes.push_back(node);\n        if (node->left) {\n           q.push(node->left);\n           sum += node->left->val;\n        }\n        if (node->right) {\n          q.push(node->right);\n          sum += node->right->val;\n        }\n      }\n      for (TreeNode* node : nodes) {\n        int val = sum - (node->left ? node->left->val : 0) \n          - (node->right ? node->right->val : 0);      \n        if (node->left) node->left->val = val;\n        if (node->right) node->right->val = val;\n      }\n    }\n    return root;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Given the&nbsp;root&nbsp;of a binary tree, replace the value of each node in the tree with the&nbsp;sum of all its cousins&#8217; values. Two nodes of a&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-10013","post","type-post","status-publish","format-standard","hentry","category-uncategorized","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/10013","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=10013"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/10013\/revisions"}],"predecessor-version":[{"id":10016,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/10013\/revisions\/10016"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=10013"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=10013"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=10013"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}