Skip to content

Commit d1ef183

Browse files
committed
feat: add solutions to lcp problem: No.44
1 parent 04292d8 commit d1ef183

File tree

5 files changed

+195
-1
lines changed

5 files changed

+195
-1
lines changed

lcp/LCP 44. 开幕式焰火/README.md

+102-1
Original file line numberDiff line numberDiff line change
@@ -49,15 +49,116 @@
4949
<!-- 这里可写当前语言的特殊实现逻辑 -->
5050

5151
```python
52-
52+
# Definition for a binary tree node.
53+
# class TreeNode:
54+
# def __init__(self, x):
55+
# self.val = x
56+
# self.left = None
57+
# self.right = None
58+
class Solution:
59+
def numColor(self, root: TreeNode) -> int:
60+
s = set()
61+
62+
def dfs(root):
63+
if root:
64+
s.add(root.val)
65+
dfs(root.left)
66+
dfs(root.right)
67+
68+
dfs(root)
69+
return len(s)
5370
```
5471

5572
### **Java**
5673

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

5976
```java
77+
/**
78+
* Definition for a binary tree node.
79+
* public class TreeNode {
80+
* int val;
81+
* TreeNode left;
82+
* TreeNode right;
83+
* TreeNode(int x) { val = x; }
84+
* }
85+
*/
86+
class Solution {
87+
private Set<Integer> s;
88+
89+
public int numColor(TreeNode root) {
90+
s = new HashSet<>();
91+
dfs(root);
92+
return s.size();
93+
}
94+
95+
private void dfs(TreeNode root) {
96+
if (root == null) {
97+
return;
98+
}
99+
s.add(root.val);
100+
dfs(root.left);
101+
dfs(root.right);
102+
}
103+
}
104+
```
105+
106+
### **C++**
107+
108+
```cpp
109+
/**
110+
* Definition for a binary tree node.
111+
* struct TreeNode {
112+
* int val;
113+
* TreeNode *left;
114+
* TreeNode *right;
115+
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
116+
* };
117+
*/
118+
class Solution {
119+
public:
120+
unordered_set<int> s;
121+
122+
int numColor(TreeNode* root) {
123+
dfs(root);
124+
return s.size();
125+
}
126+
127+
void dfs(TreeNode* root) {
128+
if (!root) return;
129+
s.insert(root->val);
130+
dfs(root->left);
131+
dfs(root->right);
132+
}
133+
};
134+
```
60135
136+
### **Go**
137+
138+
```go
139+
/**
140+
* Definition for a binary tree node.
141+
* type TreeNode struct {
142+
* Val int
143+
* Left *TreeNode
144+
* Right *TreeNode
145+
* }
146+
*/
147+
var s map[int]bool
148+
149+
func numColor(root *TreeNode) int {
150+
s = make(map[int]bool)
151+
dfs(root)
152+
return len(s)
153+
}
154+
155+
func dfs(root *TreeNode) {
156+
if root != nil {
157+
s[root.Val] = true
158+
dfs(root.Left)
159+
dfs(root.Right)
160+
}
161+
}
61162
```
62163

63164
### **...**
+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* struct TreeNode {
4+
* int val;
5+
* TreeNode *left;
6+
* TreeNode *right;
7+
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
8+
* };
9+
*/
10+
class Solution {
11+
public:
12+
unordered_set<int> s;
13+
14+
int numColor(TreeNode* root) {
15+
dfs(root);
16+
return s.size();
17+
}
18+
19+
void dfs(TreeNode* root) {
20+
if (!root) return;
21+
s.insert(root->val);
22+
dfs(root->left);
23+
dfs(root->right);
24+
}
25+
};
+23
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+
var s map[int]bool
10+
11+
func numColor(root *TreeNode) int {
12+
s = make(map[int]bool)
13+
dfs(root)
14+
return len(s)
15+
}
16+
17+
func dfs(root *TreeNode) {
18+
if root != nil {
19+
s[root.Val] = true
20+
dfs(root.Left)
21+
dfs(root.Right)
22+
}
23+
}
+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* public class TreeNode {
4+
* int val;
5+
* TreeNode left;
6+
* TreeNode right;
7+
* TreeNode(int x) { val = x; }
8+
* }
9+
*/
10+
class Solution {
11+
private Set<Integer> s;
12+
13+
public int numColor(TreeNode root) {
14+
s = new HashSet<>();
15+
dfs(root);
16+
return s.size();
17+
}
18+
19+
private void dfs(TreeNode root) {
20+
if (root == null) {
21+
return;
22+
}
23+
s.add(root.val);
24+
dfs(root.left);
25+
dfs(root.right);
26+
}
27+
}
+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Definition for a binary tree node.
2+
# class TreeNode:
3+
# def __init__(self, x):
4+
# self.val = x
5+
# self.left = None
6+
# self.right = None
7+
class Solution:
8+
def numColor(self, root: TreeNode) -> int:
9+
s = set()
10+
11+
def dfs(root):
12+
if root:
13+
s.add(root.val)
14+
dfs(root.left)
15+
dfs(root.right)
16+
17+
dfs(root)
18+
return len(s)

0 commit comments

Comments
 (0)