File tree 2 files changed +59
-0
lines changed
2 files changed +59
-0
lines changed Original file line number Diff line number Diff line change @@ -159,6 +159,38 @@ func dfs(root *TreeNode) {
159
159
}
160
160
```
161
161
162
+ #### Swift
163
+
164
+ ``` swift
165
+ /* class TreeNode {
166
+ * var val: Int
167
+ * var left: TreeNode?
168
+ * var right: TreeNode?
169
+ * init(_ val: Int) {
170
+ * self.val = val
171
+ * self.left = nil
172
+ * self.right = nil
173
+ * }
174
+ * }
175
+ */
176
+
177
+ class Solution {
178
+ private var uniqueColors: Set <Int > = []
179
+
180
+ func numColor (_ root : TreeNode? ) -> Int {
181
+ dfs (root)
182
+ return uniqueColors.count
183
+ }
184
+
185
+ private func dfs (_ node : TreeNode? ) {
186
+ guard let node = node else { return }
187
+ uniqueColors.insert (node.val )
188
+ dfs (node.left )
189
+ dfs (node.right )
190
+ }
191
+ }
192
+ ```
193
+
162
194
<!-- tabs:end -->
163
195
164
196
<!-- solution:end -->
Original file line number Diff line number Diff line change
1
+ /* class TreeNode {
2
+ * var val: Int
3
+ * var left: TreeNode?
4
+ * var right: TreeNode?
5
+ * init(_ val: Int) {
6
+ * self.val = val
7
+ * self.left = nil
8
+ * self.right = nil
9
+ * }
10
+ * }
11
+ */
12
+
13
+ class Solution {
14
+ private var uniqueColors : Set < Int > = [ ]
15
+
16
+ func numColor( _ root: TreeNode ? ) -> Int {
17
+ dfs ( root)
18
+ return uniqueColors. count
19
+ }
20
+
21
+ private func dfs( _ node: TreeNode ? ) {
22
+ guard let node = node else { return }
23
+ uniqueColors. insert ( node. val)
24
+ dfs ( node. left)
25
+ dfs ( node. right)
26
+ }
27
+ }
You can’t perform that action at this time.
0 commit comments