Skip to content

Commit df1a0af

Browse files
authored
feat: add solutions to lcp problem: No.10 (#1755)
LCP 10.二叉树任务调度
1 parent 1736b25 commit df1a0af

File tree

6 files changed

+236
-1
lines changed

6 files changed

+236
-1
lines changed

lcp/LCP 10. 二叉树任务调度/README.md

+125-1
Original file line numberDiff line numberDiff line change
@@ -62,15 +62,139 @@
6262
<!-- 这里可写当前语言的特殊实现逻辑 -->
6363

6464
```python
65-
65+
class Solution:
66+
def minimalExecTime(self, root: TreeNode) -> float:
67+
def dfs(root: TreeNode) -> Tuple[int, int]:
68+
if not root:
69+
return 0, 0
70+
s1, t1 = dfs(root.left)
71+
s2, t2 = dfs(root.right)
72+
return s1 + s2 + root.val, max(t1, t2, (s1 + s2) / 2) + root.val
73+
74+
return dfs(root)[1]
6675
```
6776

6877
### **Java**
6978

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

7281
```java
82+
/**
83+
* Definition for a binary tree node.
84+
* public class TreeNode {
85+
* int val;
86+
* TreeNode left;
87+
* TreeNode right;
88+
* TreeNode(int x) { val = x; }
89+
* }
90+
*/
91+
class Solution {
92+
public double minimalExecTime(TreeNode root) {
93+
return dfs(root)[1];
94+
}
95+
96+
private double[] dfs(TreeNode root) {
97+
if (root == null) {
98+
return new double[] {0, 0};
99+
}
100+
double[] left = dfs(root.left);
101+
double[] right = dfs(root.right);
102+
double s = left[0] + right[0] + root.val;
103+
double t = Math.max(Math.max(left[1], right[1]), (left[0] + right[0]) / 2) + root.val;
104+
return new double[] {s, t};
105+
}
106+
}
107+
```
108+
109+
### **C++**
110+
111+
```cpp
112+
/**
113+
* Definition for a binary tree node.
114+
* struct TreeNode {
115+
* int val;
116+
* TreeNode *left;
117+
* TreeNode *right;
118+
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
119+
* };
120+
*/
121+
class Solution {
122+
public:
123+
double minimalExecTime(TreeNode* root) {
124+
function<pair<double, double>(TreeNode*)> dfs = [&](TreeNode* root) -> pair<double, double> {
125+
if (!root) {
126+
return {0, 0};
127+
}
128+
auto [s1, t1] = dfs(root->left);
129+
auto [s2, t2] = dfs(root->right);
130+
double s = s1 + s2 + root->val;
131+
double t = max({t1, t2, (s1 + s2) / 2}) + root->val;
132+
return {s, t};
133+
};
134+
auto [_, t] = dfs(root);
135+
return t;
136+
}
137+
};
138+
```
139+
140+
### **Go**
141+
142+
```go
143+
/**
144+
* Definition for a binary tree node.
145+
* type TreeNode struct {
146+
* Val int
147+
* Left *TreeNode
148+
* Right *TreeNode
149+
* }
150+
*/
151+
func minimalExecTime(root *TreeNode) float64 {
152+
var dfs func(*TreeNode) (float64, float64)
153+
dfs = func(root *TreeNode) (float64, float64) {
154+
if root == nil {
155+
return 0, 0
156+
}
157+
s1, t1 := dfs(root.Left)
158+
s2, t2 := dfs(root.Right)
159+
s := s1 + s2 + float64(root.Val)
160+
t := math.Max(math.Max(t1, t2), (s1+s2)/2) + float64(root.Val)
161+
return s, t
162+
}
163+
_, t := dfs(root)
164+
return t
165+
}
166+
```
73167

168+
### **TypeScript**
169+
170+
```ts
171+
/**
172+
* Definition for a binary tree node.
173+
* class TreeNode {
174+
* val: number
175+
* left: TreeNode | null
176+
* right: TreeNode | null
177+
* constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
178+
* this.val = (val===undefined ? 0 : val)
179+
* this.left = (left===undefined ? null : left)
180+
* this.right = (right===undefined ? null : right)
181+
* }
182+
* }
183+
*/
184+
185+
function minimalExecTime(root: TreeNode | null): number {
186+
const dfs = (root: TreeNode | null): [number, number] => {
187+
if (root === null) {
188+
return [0, 0];
189+
}
190+
const [s1, t1] = dfs(root.left);
191+
const [s2, t2] = dfs(root.right);
192+
const s = s1 + s2 + root.val;
193+
const t = Math.max(t1, t2, (s1 + s2) / 2) + root.val;
194+
return [s, t];
195+
};
196+
return dfs(root)[1];
197+
}
74198
```
75199

76200
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
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+
double minimalExecTime(TreeNode* root) {
13+
function<pair<double, double>(TreeNode*)> dfs = [&](TreeNode* root) -> pair<double, double> {
14+
if (!root) {
15+
return {0, 0};
16+
}
17+
auto [s1, t1] = dfs(root->left);
18+
auto [s2, t2] = dfs(root->right);
19+
double s = s1 + s2 + root->val;
20+
double t = max({t1, t2, (s1 + s2) / 2}) + root->val;
21+
return {s, t};
22+
};
23+
auto [_, t] = dfs(root);
24+
return t;
25+
}
26+
};
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 minimalExecTime(root *TreeNode) float64 {
10+
var dfs func(*TreeNode) (float64, float64)
11+
dfs = func(root *TreeNode) (float64, float64) {
12+
if root == nil {
13+
return 0, 0
14+
}
15+
s1, t1 := dfs(root.Left)
16+
s2, t2 := dfs(root.Right)
17+
s := s1 + s2 + float64(root.Val)
18+
t := math.Max(math.Max(t1, t2), (s1+s2)/2) + float64(root.Val)
19+
return s, t
20+
}
21+
_, t := dfs(root)
22+
return t
23+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
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+
public double minimalExecTime(TreeNode root) {
12+
return dfs(root)[1];
13+
}
14+
15+
private double[] dfs(TreeNode root) {
16+
if (root == null) {
17+
return new double[] {0, 0};
18+
}
19+
double[] left = dfs(root.left);
20+
double[] right = dfs(root.right);
21+
double s = left[0] + right[0] + root.val;
22+
double t = Math.max(Math.max(left[1], right[1]), (left[0] + right[0]) / 2) + root.val;
23+
return new double[] {s, t};
24+
}
25+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
class Solution:
2+
def minimalExecTime(self, root: TreeNode) -> float:
3+
def dfs(root: TreeNode) -> Tuple[int, int]:
4+
if not root:
5+
return 0, 0
6+
s1, t1 = dfs(root.left)
7+
s2, t2 = dfs(root.right)
8+
return s1 + s2 + root.val, max(t1, t2, (s1 + s2) / 2) + root.val
9+
10+
return dfs(root)[1]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* class TreeNode {
4+
* val: number
5+
* left: TreeNode | null
6+
* right: TreeNode | null
7+
* constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
8+
* this.val = (val===undefined ? 0 : val)
9+
* this.left = (left===undefined ? null : left)
10+
* this.right = (right===undefined ? null : right)
11+
* }
12+
* }
13+
*/
14+
15+
function minimalExecTime(root: TreeNode | null): number {
16+
const dfs = (root: TreeNode | null): [number, number] => {
17+
if (root === null) {
18+
return [0, 0];
19+
}
20+
const [s1, t1] = dfs(root.left);
21+
const [s2, t2] = dfs(root.right);
22+
const s = s1 + s2 + root.val;
23+
const t = Math.max(t1, t2, (s1 + s2) / 2) + root.val;
24+
return [s, t];
25+
};
26+
return dfs(root)[1];
27+
}

0 commit comments

Comments
 (0)