Skip to content

Commit 85457b6

Browse files
committed
feat: add solutions to lc problem: No.1973
No.1973.Count Nodes Equal to Sum of Descendants
1 parent 9a8dc81 commit 85457b6

File tree

6 files changed

+347
-2
lines changed

6 files changed

+347
-2
lines changed

solution/1900-1999/1973.Count Nodes Equal to Sum of Descendants/README.md

+126-1
Original file line numberDiff line numberDiff line change
@@ -53,22 +53,147 @@
5353

5454
<!-- 这里可写通用的实现逻辑 -->
5555

56+
**方法一:递归**
57+
58+
我们设计一个函数 $dfs(root)$,该函数返回以 $root$ 为根节点的子树的所有节点值之和。函数 $dfs(root)$ 的执行过程如下:
59+
60+
- 如果 $root$ 为空,返回 $0$;
61+
- 否则,我们递归地计算 $root$ 的左子树和右子树的节点值之和,记为 $l$ 和 $r$;如果 $l + r = root.val$,说明以 $root$ 为根节点的子树满足条件,我们将答案加 $1$;最后,返回 $root.val + l + r$。
62+
63+
然后我们调用函数 $dfs(root)$,返回答案即可。
64+
65+
时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 是二叉树的节点数。
66+
5667
<!-- tabs:start -->
5768

5869
### **Python3**
5970

6071
<!-- 这里可写当前语言的特殊实现逻辑 -->
6172

6273
```python
63-
74+
# Definition for a binary tree node.
75+
# class TreeNode:
76+
# def __init__(self, val=0, left=None, right=None):
77+
# self.val = val
78+
# self.left = left
79+
# self.right = right
80+
class Solution:
81+
def equalToDescendants(self, root: Optional[TreeNode]) -> int:
82+
def dfs(root):
83+
if root is None:
84+
return 0
85+
l, r = dfs(root.left), dfs(root.right)
86+
if l + r == root.val:
87+
nonlocal ans
88+
ans += 1
89+
return root.val + l + r
90+
91+
ans = 0
92+
dfs(root)
93+
return ans
6494
```
6595

6696
### **Java**
6797

6898
<!-- 这里可写当前语言的特殊实现逻辑 -->
6999

70100
```java
101+
/**
102+
* Definition for a binary tree node.
103+
* public class TreeNode {
104+
* int val;
105+
* TreeNode left;
106+
* TreeNode right;
107+
* TreeNode() {}
108+
* TreeNode(int val) { this.val = val; }
109+
* TreeNode(int val, TreeNode left, TreeNode right) {
110+
* this.val = val;
111+
* this.left = left;
112+
* this.right = right;
113+
* }
114+
* }
115+
*/
116+
class Solution {
117+
private int ans;
118+
119+
public int equalToDescendants(TreeNode root) {
120+
dfs(root);
121+
return ans;
122+
}
123+
124+
private int dfs(TreeNode root) {
125+
if (root == null) {
126+
return 0;
127+
}
128+
int l = dfs(root.left);
129+
int r = dfs(root.right);
130+
if (l + r == root.val) {
131+
++ans;
132+
}
133+
return root.val + l + r;
134+
}
135+
}
136+
```
137+
138+
### **C++**
139+
140+
```cpp
141+
/**
142+
* Definition for a binary tree node.
143+
* struct TreeNode {
144+
* int val;
145+
* TreeNode *left;
146+
* TreeNode *right;
147+
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
148+
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
149+
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
150+
* };
151+
*/
152+
class Solution {
153+
public:
154+
int equalToDescendants(TreeNode* root) {
155+
int ans = 0;
156+
function<long long(TreeNode*)> dfs = [&](TreeNode* root) -> long long {
157+
if (!root) {
158+
return 0;
159+
}
160+
auto l = dfs(root->left);
161+
auto r = dfs(root->right);
162+
ans += l + r == root->val;
163+
return root->val + l + r;
164+
};
165+
dfs(root);
166+
return ans;
167+
}
168+
};
169+
```
71170
171+
### **Go**
172+
173+
```go
174+
/**
175+
* Definition for a binary tree node.
176+
* type TreeNode struct {
177+
* Val int
178+
* Left *TreeNode
179+
* Right *TreeNode
180+
* }
181+
*/
182+
func equalToDescendants(root *TreeNode) (ans int) {
183+
var dfs func(*TreeNode) int
184+
dfs = func(root *TreeNode) int {
185+
if root == nil {
186+
return 0
187+
}
188+
l, r := dfs(root.Left), dfs(root.Right)
189+
if l+r == root.Val {
190+
ans++
191+
}
192+
return root.Val + l + r
193+
}
194+
dfs(root)
195+
return
196+
}
72197
```
73198

74199
### **...**

solution/1900-1999/1973.Count Nodes Equal to Sum of Descendants/README_EN.md

+115-1
Original file line numberDiff line numberDiff line change
@@ -51,13 +51,127 @@ For the node with value 0: The sum of its descendants is 0 since it has no desce
5151
### **Python3**
5252

5353
```python
54-
54+
# Definition for a binary tree node.
55+
# class TreeNode:
56+
# def __init__(self, val=0, left=None, right=None):
57+
# self.val = val
58+
# self.left = left
59+
# self.right = right
60+
class Solution:
61+
def equalToDescendants(self, root: Optional[TreeNode]) -> int:
62+
def dfs(root):
63+
if root is None:
64+
return 0
65+
l, r = dfs(root.left), dfs(root.right)
66+
if l + r == root.val:
67+
nonlocal ans
68+
ans += 1
69+
return root.val + l + r
70+
71+
ans = 0
72+
dfs(root)
73+
return ans
5574
```
5675

5776
### **Java**
5877

5978
```java
79+
/**
80+
* Definition for a binary tree node.
81+
* public class TreeNode {
82+
* int val;
83+
* TreeNode left;
84+
* TreeNode right;
85+
* TreeNode() {}
86+
* TreeNode(int val) { this.val = val; }
87+
* TreeNode(int val, TreeNode left, TreeNode right) {
88+
* this.val = val;
89+
* this.left = left;
90+
* this.right = right;
91+
* }
92+
* }
93+
*/
94+
class Solution {
95+
private int ans;
96+
97+
public int equalToDescendants(TreeNode root) {
98+
dfs(root);
99+
return ans;
100+
}
101+
102+
private int dfs(TreeNode root) {
103+
if (root == null) {
104+
return 0;
105+
}
106+
int l = dfs(root.left);
107+
int r = dfs(root.right);
108+
if (l + r == root.val) {
109+
++ans;
110+
}
111+
return root.val + l + r;
112+
}
113+
}
114+
```
115+
116+
### **C++**
117+
118+
```cpp
119+
/**
120+
* Definition for a binary tree node.
121+
* struct TreeNode {
122+
* int val;
123+
* TreeNode *left;
124+
* TreeNode *right;
125+
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
126+
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
127+
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
128+
* };
129+
*/
130+
class Solution {
131+
public:
132+
int equalToDescendants(TreeNode* root) {
133+
int ans = 0;
134+
function<long long(TreeNode*)> dfs = [&](TreeNode* root) -> long long {
135+
if (!root) {
136+
return 0;
137+
}
138+
auto l = dfs(root->left);
139+
auto r = dfs(root->right);
140+
ans += l + r == root->val;
141+
return root->val + l + r;
142+
};
143+
dfs(root);
144+
return ans;
145+
}
146+
};
147+
```
60148
149+
### **Go**
150+
151+
```go
152+
/**
153+
* Definition for a binary tree node.
154+
* type TreeNode struct {
155+
* Val int
156+
* Left *TreeNode
157+
* Right *TreeNode
158+
* }
159+
*/
160+
func equalToDescendants(root *TreeNode) (ans int) {
161+
var dfs func(*TreeNode) int
162+
dfs = func(root *TreeNode) int {
163+
if root == nil {
164+
return 0
165+
}
166+
l, r := dfs(root.Left), dfs(root.Right)
167+
if l+r == root.Val {
168+
ans++
169+
}
170+
return root.Val + l + r
171+
}
172+
dfs(root)
173+
return
174+
}
61175
```
62176

63177
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* struct TreeNode {
4+
* int val;
5+
* TreeNode *left;
6+
* TreeNode *right;
7+
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
8+
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
9+
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
10+
* };
11+
*/
12+
class Solution {
13+
public:
14+
int equalToDescendants(TreeNode* root) {
15+
int ans = 0;
16+
function<long long(TreeNode*)> dfs = [&](TreeNode* root) -> long long {
17+
if (!root) {
18+
return 0;
19+
}
20+
auto l = dfs(root->left);
21+
auto r = dfs(root->right);
22+
ans += l + r == root->val;
23+
return root->val + l + r;
24+
};
25+
dfs(root);
26+
return ans;
27+
}
28+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* type TreeNode struct {
4+
* Val int
5+
* Left *TreeNode
6+
* Right *TreeNode
7+
* }
8+
*/
9+
func equalToDescendants(root *TreeNode) (ans int) {
10+
var dfs func(*TreeNode) int
11+
dfs = func(root *TreeNode) int {
12+
if root == nil {
13+
return 0
14+
}
15+
l, r := dfs(root.Left), dfs(root.Right)
16+
if l+r == root.Val {
17+
ans++
18+
}
19+
return root.Val + l + r
20+
}
21+
dfs(root)
22+
return
23+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* public class TreeNode {
4+
* int val;
5+
* TreeNode left;
6+
* TreeNode right;
7+
* TreeNode() {}
8+
* TreeNode(int val) { this.val = val; }
9+
* TreeNode(int val, TreeNode left, TreeNode right) {
10+
* this.val = val;
11+
* this.left = left;
12+
* this.right = right;
13+
* }
14+
* }
15+
*/
16+
class Solution {
17+
private int ans;
18+
19+
public int equalToDescendants(TreeNode root) {
20+
dfs(root);
21+
return ans;
22+
}
23+
24+
private int dfs(TreeNode root) {
25+
if (root == null) {
26+
return 0;
27+
}
28+
int l = dfs(root.left);
29+
int r = dfs(root.right);
30+
if (l + r == root.val) {
31+
++ans;
32+
}
33+
return root.val + l + r;
34+
}
35+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Definition for a binary tree node.
2+
# class TreeNode:
3+
# def __init__(self, val=0, left=None, right=None):
4+
# self.val = val
5+
# self.left = left
6+
# self.right = right
7+
class Solution:
8+
def equalToDescendants(self, root: Optional[TreeNode]) -> int:
9+
def dfs(root):
10+
if root is None:
11+
return 0
12+
l, r = dfs(root.left), dfs(root.right)
13+
if l + r == root.val:
14+
nonlocal ans
15+
ans += 1
16+
return root.val + l + r
17+
18+
ans = 0
19+
dfs(root)
20+
return ans

0 commit comments

Comments
 (0)