@@ -50,13 +50,139 @@ For the node with value 6: The average of its subtree is 6 / 1 = 6.
50
50
### ** Python3**
51
51
52
52
``` python
53
-
53
+ # Definition for a binary tree node.
54
+ # class TreeNode:
55
+ # def __init__(self, val=0, left=None, right=None):
56
+ # self.val = val
57
+ # self.left = left
58
+ # self.right = right
59
+ class Solution :
60
+ def averageOfSubtree (self , root : Optional[TreeNode]) -> int :
61
+ def dfs (root ):
62
+ if root is None :
63
+ return 0 , 0
64
+ ls, ln = dfs(root.left)
65
+ rs, rn = dfs(root.right)
66
+ s = ls + rs + root.val
67
+ n = ln + rn + 1
68
+ if s // n == root.val:
69
+ nonlocal ans
70
+ ans += 1
71
+ return s, n
72
+
73
+ ans = 0
74
+ dfs(root)
75
+ return ans
54
76
```
55
77
56
78
### ** Java**
57
79
58
80
``` java
81
+ /**
82
+ * Definition for a binary tree node.
83
+ * public class TreeNode {
84
+ * int val;
85
+ * TreeNode left;
86
+ * TreeNode right;
87
+ * TreeNode() {}
88
+ * TreeNode(int val) { this.val = val; }
89
+ * TreeNode(int val, TreeNode left, TreeNode right) {
90
+ * this.val = val;
91
+ * this.left = left;
92
+ * this.right = right;
93
+ * }
94
+ * }
95
+ */
96
+ class Solution {
97
+ private int ans;
98
+
99
+ public int averageOfSubtree (TreeNode root ) {
100
+ ans = 0 ;
101
+ dfs(root);
102
+ return ans;
103
+ }
104
+
105
+ private int [] dfs (TreeNode root ) {
106
+ if (root == null ) {
107
+ return new int []{0 , 0 };
108
+ }
109
+ int [] l = dfs(root. left);
110
+ int [] r = dfs(root. right);
111
+ int s = l[0 ] + r[0 ] + root. val;
112
+ int n = l[1 ] + r[1 ] + 1 ;
113
+ if (s / n == root. val) {
114
+ ++ ans;
115
+ }
116
+ return new int []{s, n};
117
+ }
118
+ }
119
+ ```
120
+
121
+ ### ** C++**
122
+
123
+ ``` cpp
124
+ /* *
125
+ * Definition for a binary tree node.
126
+ * struct TreeNode {
127
+ * int val;
128
+ * TreeNode *left;
129
+ * TreeNode *right;
130
+ * TreeNode() : val(0), left(nullptr), right(nullptr) {}
131
+ * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
132
+ * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
133
+ * };
134
+ */
135
+ class Solution {
136
+ public:
137
+ int ans;
138
+ int averageOfSubtree(TreeNode* root) {
139
+ ans = 0;
140
+ dfs(root);
141
+ return ans;
142
+ }
143
+
144
+ vector<int> dfs(TreeNode* root) {
145
+ if (!root) return {0, 0};
146
+ auto l = dfs(root->left);
147
+ auto r = dfs(root->right);
148
+ int s = l[0] + r[0] + root->val;
149
+ int n = l[1] + r[1] + 1;
150
+ if (s / n == root->val) ++ans;
151
+ return {s, n};
152
+ }
153
+ };
154
+ ```
59
155
156
+ ### ** Go**
157
+
158
+ ``` go
159
+ /* *
160
+ * Definition for a binary tree node.
161
+ * type TreeNode struct {
162
+ * Val int
163
+ * Left *TreeNode
164
+ * Right *TreeNode
165
+ * }
166
+ */
167
+ func averageOfSubtree (root *TreeNode ) int {
168
+ ans := 0
169
+ var dfs func (*TreeNode) (int , int )
170
+ dfs = func (root *TreeNode) (int , int ) {
171
+ if root == nil {
172
+ return 0 , 0
173
+ }
174
+ ls , ln := dfs (root.Left )
175
+ rs , rn := dfs (root.Right )
176
+ s := ls + rs + root.Val
177
+ n := ln + rn + 1
178
+ if s/n == root.Val {
179
+ ans++
180
+ }
181
+ return s, n
182
+ }
183
+ dfs (root)
184
+ return ans
185
+ }
60
186
```
61
187
62
188
### ** TypeScript**
0 commit comments