Skip to content

Commit

Permalink
feat: add solutions to lc problem: No.1315 (#2461)
Browse files Browse the repository at this point in the history
No.1315.Sum of Nodes with Even-Valued Grandparent
  • Loading branch information
yanglbme authored Mar 19, 2024
1 parent 04fe700 commit 9a66cae
Show file tree
Hide file tree
Showing 7 changed files with 273 additions and 205 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,17 @@

## 解法

### 方法一
### 方法一:DFS

我们设计一个函数 $dfs(root, x)$,表示以 $root$ 为根节点,并且 $root$ 的父节点的值为 $x$ 的子树中,满足条件的节点的值之和。那么答案就是 $dfs(root, 1)$。

函数 $dfs(root, x)$ 的执行过程如下:

- 如果 $root$ 为空,返回 $0$。
- 否则,我们递归计算 $root$ 的左子树和右子树的答案,即 $dfs(root.left, root.val)$ 和 $dfs(root.right, root.val)$,累加到答案中。如果 $x$ 为偶数,此时我们判断 $root$ 的左孩子和右孩子是否存在,如果存在,我们将它们的值累加到答案中。
- 最后返回答案。

时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 为节点个数。

<!-- tabs:start -->

Expand All @@ -51,22 +61,18 @@
# 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
def dfs(root: TreeNode, x: int) -> int:
if root is None:
return 0
ans = dfs(root.left, root.val) + dfs(root.right, root.val)
if x % 2 == 0:
if root.left:
ans += root.left.val
if root.right:
ans += root.right.val
return ans

return dfs(root, 1)
```

```java
Expand All @@ -86,29 +92,24 @@ class Solution:
* }
*/
class Solution {
private int res;

public int sumEvenGrandparent(TreeNode root) {
res = 0;
dfs(root, root.left);
dfs(root, root.right);
return res;
return dfs(root, 1);
}

private void dfs(TreeNode g, TreeNode p) {
if (p == null) {
return;
private int dfs(TreeNode root, int x) {
if (root == null) {
return 0;
}
if (g.val % 2 == 0) {
if (p.left != null) {
res += p.left.val;
int ans = dfs(root.left, root.val) + dfs(root.right, root.val);
if (x % 2 == 0) {
if (root.left != null) {
ans += root.left.val;
}
if (p.right != null) {
res += p.right.val;
if (root.right != null) {
ans += root.right.val;
}
}
dfs(p, p.left);
dfs(p, p.right);
return ans;
}
}
```
Expand All @@ -127,23 +128,23 @@ class Solution {
*/
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);
function<int(TreeNode*, int)> dfs = [&](TreeNode* root, int x) {
if (!root) {
return 0;
}
int ans = dfs(root->left, root->val) + dfs(root->right, root->val);
if (x % 2 == 0) {
if (root->left) {
ans += root->left->val;
}
if (root->right) {
ans += root->right->val;
}
}
return ans;
};
return dfs(root, 1);
}
};
```
Expand All @@ -157,30 +158,56 @@ public:
* 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
var dfs func(*TreeNode, int) int
dfs = func(root *TreeNode, x int) int {
if root == nil {
return 0
}
if p.Right != nil {
res += p.Right.Val
ans := dfs(root.Left, root.Val) + dfs(root.Right, root.Val)
if x%2 == 0 {
if root.Left != nil {
ans += root.Left.Val
}
if root.Right != nil {
ans += root.Right.Val
}
}
return ans
}
dfs(p, p.Left)
dfs(p, p.Right)
return dfs(root, 1)
}
```

```ts
/**
* Definition for a binary tree node.
* class TreeNode {
* val: number
* left: TreeNode | null
* right: TreeNode | null
* constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
* this.val = (val===undefined ? 0 : val)
* this.left = (left===undefined ? null : left)
* this.right = (right===undefined ? null : right)
* }
* }
*/

function sumEvenGrandparent(root: TreeNode | null): number {
const dfs = (root: TreeNode | null, x: number): number => {
if (!root) {
return 0;
}
const { val, left, right } = root;
let ans = dfs(left, val) + dfs(right, val);
if (x % 2 === 0) {
ans += left?.val ?? 0;
ans += right?.val ?? 0;
}
return ans;
};
return dfs(root, 1);
}
```

Expand Down
Loading

0 comments on commit 9a66cae

Please sign in to comment.