当前位置:网站首页>1315. Sum of nodes with even grandfather node value (medium)
1315. Sum of nodes with even grandfather node value (medium)
2022-04-22 08:40:00 【weixin_ forty-six million two hundred and seventy-two thousand 】
1315. The grandfather node value is an even number of nodes and
Here is a binary tree for you , Please return the sum of the values of all nodes that meet the following conditions :
The value of the grandfather node of this node is even .( The grandfather node of a node refers to the parent node of the parent node of the node .)
If there is no node with even value of grandfather node , Then the return 0 .
Example :

Input :root = [6,7,8,2,7,1,3,9,null,1,4,null,null,null,5]
Output :18
explain : The value of the grandfather node of the red node in the figure is even , Blue nodes are the grandfather nodes of these red nodes .
Ideas
Save the information of three nodes , They are Grandfather nodes , Parent node and current node
- If the value of the grandfather node satisfies the even condition , be sum Add the value of the current node
- The next recursion , The parent node acts as the parent node , The current node acts as the parent node , Achieve progressive progress layer by layer
Source code
class Solution {
public:
int sum = 0;
int sumEvenGrandparent(TreeNode* root) {
dfs(root,NULL,NULL);
return sum;
}
void dfs(TreeNode* root, TreeNode* parent, TreeNode* grandparent) {
if (root == NULL) {
return;
}
if (grandparent != NULL && grandparent->val % 2 == 0) {
sum += root->val;
}
dfs(root->left,root,parent);
dfs(root->right,root,parent);
}
};
Official explanation
class Solution {
public:
int sum = 0;
void dfs(TreeNode* grandparent, TreeNode* parent, TreeNode* node) {
if (node == nullptr) {
return;
}
if (grandparent->val % 2 == 0) {
sum += node->val;
}
dfs(parent,node,node->left);
dfs(parent,node,node->right);
}
int sumEvenGrandparent(TreeNode* root) {
if (root == nullptr) {
return 0;
}
if (root->left) {
dfs(root,root->left,root->left->left);
dfs(root,root->left,root->left->right);
}
if (root->right) {
dfs(root,root->right,root->right->left);
dfs(root,root->right,root->right->right);
}
return sum;
}
};
版权声明
本文为[weixin_ forty-six million two hundred and seventy-two thousand ]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204220742290863.html
边栏推荐
- Use the method of onlayoutchangelistener to solve the problem of gettop = 0
- [no very low code] low code platform development diary, SQL programming of low code platform
- cesium 采集地形高度,采集模型高度 (异步方法,适合数据较多的时候)
- SQL database multiple choice question (2)
- OSPF class IV, class V and class VII LSA details
- The PS - EF query process PID in the shell script always returns an exception
- pictures rotating
- Level 2: ACL access control list
- Constructor and toString
- 空心字母金字塔
猜你喜欢

Pointer and string

The domestic cloud security market has exceeded 10 billion yuan. What is the future development trend?

shell脚本学习笔记——正则表达式

Spark SQL gets the element at an index of the array

Level 3: node status check, data view and update

Seven crimes of hackers in social engineering -- hooking

第1关:创建/删除节点

MATLAB提示:要使用 ‘xxx函数‘,则必须授权、安装并启用了以下产品:xxx toolbox

redis 简单使用
![[paper reading] [3D target detection] pvgnet](/img/fc/7fd22bbc1aaf4bc5ae38543cd03ae3.png)
[paper reading] [3D target detection] pvgnet
随机推荐
shell脚本学习笔记——shell对文件的操作sed
pictures rotating
MaterialApp
字符串替换相关题目(合并数组)
第2关:多态
Go语言基础(1)
Winsock编程接口实验:实现ipconfig
Algorithm -- delete the penultimate node of the linked list (kotlin)
第3关:节点配额及其他命令
JS judge the element to the top and fix it
shell笔记
社会工程学之黑客七宗罪——贪婪(死亡之PING)
客户端与服务器项目3
Viewpager comprehensive summary
Client server communication project 2
shell脚本学习笔记——正则表达式
leaflet、cesium加载百度地图,加载自定义样式百度地图
The fluent modul class and JSON are converted to each other
94. 二叉树的中序遍历(Easy)
[paper reading] [3D object detection] voxel set transformer: a set to set approach to 3D object detection from point clouds