diff --git a/solution/1300-1399/1315.Sum of Nodes with Even-Valued Grandparent/README.md b/solution/1300-1399/1315.Sum of Nodes with Even-Valued Grandparent/README.md index 72a9bc7e4a0c6..c7a6cd1148156 100644 --- a/solution/1300-1399/1315.Sum of Nodes with Even-Valued Grandparent/README.md +++ b/solution/1300-1399/1315.Sum of Nodes with Even-Valued Grandparent/README.md @@ -34,11 +34,12 @@
  • 每个节点的值在 1 到 100 之间。
  • - ## 解法 +深度优先搜索。 + ### **Python3** @@ -46,7 +47,30 @@ ```python - +# Definition for a binary tree node. +# class TreeNode: +# def __init__(self, val=0, left=None, right=None): +# self.val = val +# self.left = left +# self.right = right +class Solution: + def sumEvenGrandparent(self, root: TreeNode) -> int: + self.res = 0 + + def dfs(g, p): + if p is None: + return + if g.val % 2 == 0: + if p.left: + self.res += p.left.val + if p.right: + self.res += p.right.val + dfs(p, p.left) + dfs(p, p.right) + + dfs(root, root.left) + dfs(root, root.right) + return self.res ``` ### **Java** @@ -54,7 +78,123 @@ ```java +/** + * Definition for a binary tree node. + * public class TreeNode { + * int val; + * TreeNode left; + * TreeNode right; + * TreeNode() {} + * TreeNode(int val) { this.val = val; } + * TreeNode(int val, TreeNode left, TreeNode right) { + * this.val = val; + * this.left = left; + * this.right = right; + * } + * } + */ +class Solution { + private int res; + + public int sumEvenGrandparent(TreeNode root) { + res = 0; + dfs(root, root.left); + dfs(root, root.right); + return res; + } + + private void dfs(TreeNode g, TreeNode p) { + if (p == null) { + return; + } + if (g.val % 2 == 0) { + if (p.left != null) { + res += p.left.val; + } + if (p.right != null) { + res += p.right.val; + } + } + dfs(p, p.left); + dfs(p, p.right); + } +} +``` + +### **C++** + +```cpp +/** + * Definition for a binary tree node. + * struct TreeNode { + * int val; + * TreeNode *left; + * TreeNode *right; + * TreeNode() : val(0), left(nullptr), right(nullptr) {} + * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} + * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} + * }; + */ +class Solution { +public: + int res; + + int sumEvenGrandparent(TreeNode* root) { + res = 0; + dfs(root, root->left); + dfs(root, root->right); + return res; + } + + void dfs(TreeNode* g, TreeNode* p) { + if (!p) return; + if (g->val % 2 == 0) + { + if (p->left) res += p->left->val; + if (p->right) res += p->right->val; + } + dfs(p, p->left); + dfs(p, p->right); + } +}; +``` +### **Go** + +```go +/** + * Definition for a binary tree node. + * type TreeNode struct { + * Val int + * Left *TreeNode + * Right *TreeNode + * } + */ + +var res int + +func sumEvenGrandparent(root *TreeNode) int { + res = 0 + dfs(root, root.Left) + dfs(root, root.Right) + return res +} + +func dfs(g, p *TreeNode) { + if p == nil { + return + } + if g.Val%2 == 0 { + if p.Left != nil { + res += p.Left.Val + } + if p.Right != nil { + res += p.Right.Val + } + } + dfs(p, p.Left) + dfs(p, p.Right) +} ``` ### **...** diff --git a/solution/1300-1399/1315.Sum of Nodes with Even-Valued Grandparent/README_EN.md b/solution/1300-1399/1315.Sum of Nodes with Even-Valued Grandparent/README_EN.md index 54fcc89e28209..bb89459607d55 100644 --- a/solution/1300-1399/1315.Sum of Nodes with Even-Valued Grandparent/README_EN.md +++ b/solution/1300-1399/1315.Sum of Nodes with Even-Valued Grandparent/README_EN.md @@ -6,22 +6,14 @@

    Given a binary tree, return the sum of values of nodes with even-valued grandparent.  (A grandparent of a node is the parent of its parent, if it exists.)

    - -

    If there are no nodes with an even-valued grandparent, return 0.

    - -

     

    Example 1:

    - -

    - -
     
     Input: root = [6,7,8,2,7,1,3,9,null,1,4,null,null,null,5]
    @@ -32,14 +24,10 @@
     
     
    - -

     

    Constraints:

    - -