{"id":6068,"date":"2020-01-11T10:24:06","date_gmt":"2020-01-11T18:24:06","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=6068"},"modified":"2020-12-29T20:44:27","modified_gmt":"2020-12-30T04:44:27","slug":"leetcode-1315-sum-of-nodes-with-even-valued-grandparent","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/tree\/leetcode-1315-sum-of-nodes-with-even-valued-grandparent\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1315. Sum of Nodes with Even-Valued Grandparent"},"content":{"rendered":"\n<p>Given a binary tree, return the sum of values of nodes with even-valued grandparent.&nbsp; (A&nbsp;<em>grandparent<\/em>&nbsp;of a node is the parent of its parent, if it exists.)<\/p>\n\n\n\n<p>If there are no nodes with an even-valued grandparent, return&nbsp;<code>0<\/code>.<\/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\/24\/1473_ex1.png\" alt=\"\"\/><\/figure>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> root = [6,7,8,2,7,1,3,9,null,1,4,null,null,null,5]\n<strong>Output:<\/strong> 18\n<strong>Explanation:<\/strong> The red nodes are the nodes with even-value grandparent while the blue nodes are the even-value grandparents.\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<p><strong>Solution: Recursion<\/strong><\/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++\">\/\/ Author: Huahua\nclass Solution {\npublic:\n  int sumEvenGrandparent(TreeNode* root, int p = 1, int gp = 1) {    \n    if (!root) return 0;\n    return sumEvenGrandparent(root-&gt;left, root-&gt;val, p)\n           + sumEvenGrandparent(root-&gt;right, root-&gt;val, p) \n           + (gp &amp; 1 ? 0 : root-&gt;val);\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Given a binary tree, return the sum of values of nodes with even-valued grandparent.&nbsp; (A&nbsp;grandparent&nbsp;of a node is the parent of its parent, if it&#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":[528,177,17,28],"class_list":["post-6068","post","type-post","status-publish","format-standard","hentry","category-tree","tag-grandparent","tag-medium","tag-recursion","tag-tree","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6068","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=6068"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6068\/revisions"}],"predecessor-version":[{"id":6070,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6068\/revisions\/6070"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=6068"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=6068"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=6068"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}