<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>grandparent Archives - Huahua&#039;s Tech Road</title>
	<atom:link href="https://zxi.mytechroad.com/blog/tag/grandparent/feed/" rel="self" type="application/rss+xml" />
	<link>https://zxi.mytechroad.com/blog/tag/grandparent/</link>
	<description></description>
	<lastBuildDate>Wed, 30 Dec 2020 04:44:27 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.0.8</generator>

<image>
	<url>https://zxi.mytechroad.com/blog/wp-content/uploads/2017/09/cropped-photo-32x32.jpg</url>
	<title>grandparent Archives - Huahua&#039;s Tech Road</title>
	<link>https://zxi.mytechroad.com/blog/tag/grandparent/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>花花酱 LeetCode 1315. Sum of Nodes with Even-Valued Grandparent</title>
		<link>https://zxi.mytechroad.com/blog/tree/leetcode-1315-sum-of-nodes-with-even-valued-grandparent/</link>
					<comments>https://zxi.mytechroad.com/blog/tree/leetcode-1315-sum-of-nodes-with-even-valued-grandparent/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sat, 11 Jan 2020 18:24:06 +0000</pubDate>
				<category><![CDATA[Tree]]></category>
		<category><![CDATA[grandparent]]></category>
		<category><![CDATA[medium]]></category>
		<category><![CDATA[recursion]]></category>
		<category><![CDATA[tree]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=6068</guid>

					<description><![CDATA[<p>Given a binary tree, return the sum of values of nodes with even-valued grandparent.&#160; (A&#160;grandparent&#160;of a node is the parent of its parent, if it&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/tree/leetcode-1315-sum-of-nodes-with-even-valued-grandparent/">花花酱 LeetCode 1315. Sum of Nodes with Even-Valued Grandparent</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<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>



<p>If there are no nodes with an even-valued grandparent, return&nbsp;<code>0</code>.</p>



<p><strong>Example 1:</strong></p>



<figure class="wp-block-image"><img src="https://assets.leetcode.com/uploads/2019/07/24/1473_ex1.png" alt=""/></figure>



<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]
<strong>Output:</strong> 18
<strong>Explanation:</strong> The red nodes are the nodes with even-value grandparent while the blue nodes are the even-value grandparents.
</pre>



<p><strong>Constraints:</strong></p>



<ul><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>



<p><strong>Solution: Recursion</strong></p>



<p>Time complexity: O(n)<br>Space complexity: O(n)</p>



<div class="responsive-tabs">
<h2 class="tabtitle">C++</h2>
<div class="tabcontent">

<pre class="crayon-plain-tag">// Author: Huahua
class Solution {
public:
  int sumEvenGrandparent(TreeNode* root, int p = 1, int gp = 1) {    
    if (!root) return 0;
    return sumEvenGrandparent(root-&amp;gt;left, root-&amp;gt;val, p)
           + sumEvenGrandparent(root-&amp;gt;right, root-&amp;gt;val, p) 
           + (gp &amp;amp; 1 ? 0 : root-&amp;gt;val);
  }
};</pre>
</div></div>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/tree/leetcode-1315-sum-of-nodes-with-even-valued-grandparent/">花花酱 LeetCode 1315. Sum of Nodes with Even-Valued Grandparent</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://zxi.mytechroad.com/blog/tree/leetcode-1315-sum-of-nodes-with-even-valued-grandparent/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
