Skip to content

Commit 53fea67

Browse files
authored
feat: add solutions to lc problem: No.2641 (doocs#2297)
No.2641.Cousins in Binary Tree II
1 parent e644d8b commit 53fea67

File tree

12 files changed

+411
-415
lines changed

12 files changed

+411
-415
lines changed

solution/2600-2699/2641.Cousins in Binary Tree II/README.md

+133-139
Large diffs are not rendered by default.

solution/2600-2699/2641.Cousins in Binary Tree II/README_EN.md

+147-139
Large diffs are not rendered by default.

solution/2600-2699/2641.Cousins in Binary Tree II/Solution.cpp

+28-28
Original file line numberDiff line numberDiff line change
@@ -12,36 +12,36 @@
1212
class Solution {
1313
public:
1414
TreeNode* replaceValueInTree(TreeNode* root) {
15-
vector<int> s;
16-
function<void(TreeNode*, int)> dfs1 = [&](TreeNode* root, int d) {
17-
if (!root) {
18-
return;
19-
}
20-
if (s.size() <= d) {
21-
s.push_back(0);
22-
}
23-
s[d] += root->val;
24-
dfs1(root->left, d + 1);
25-
dfs1(root->right, d + 1);
26-
};
27-
function<void(TreeNode*, int)> dfs2 = [&](TreeNode* root, int d) {
28-
if (!root) {
29-
return;
30-
}
31-
int l = root->left ? root->left->val : 0;
32-
int r = root->right ? root->right->val : 0;
33-
if (root->left) {
34-
root->left->val = s[d] - l - r;
35-
}
36-
if (root->right) {
37-
root->right->val = s[d] - l - r;
38-
}
39-
dfs2(root->left, d + 1);
40-
dfs2(root->right, d + 1);
41-
};
15+
memset(s, 0, sizeof(s));
4216
dfs1(root, 0);
4317
root->val = 0;
44-
dfs2(root, 1);
18+
dfs2(root, 0);
4519
return root;
4620
}
21+
22+
private:
23+
int s[100010];
24+
void dfs1(TreeNode* root, int depth) {
25+
if (!root) {
26+
return;
27+
}
28+
s[depth] += root->val;
29+
dfs1(root->left, depth + 1);
30+
dfs1(root->right, depth + 1);
31+
};
32+
33+
void dfs2(TreeNode* root, int depth) {
34+
int l = root->left ? root->left->val : 0;
35+
int r = root->right ? root->right->val : 0;
36+
int sub = l + r;
37+
++depth;
38+
if (root->left) {
39+
root->left->val = s[depth] - sub;
40+
dfs2(root->left, depth);
41+
}
42+
if (root->right) {
43+
root->right->val = s[depth] - sub;
44+
dfs2(root->right, depth);
45+
}
46+
};
4747
};

solution/2600-2699/2641.Cousins in Binary Tree II/Solution.go

+13-14
Original file line numberDiff line numberDiff line change
@@ -9,40 +9,39 @@
99
func replaceValueInTree(root *TreeNode) *TreeNode {
1010
s := []int{}
1111
var dfs1 func(*TreeNode, int)
12-
dfs1 = func(root *TreeNode, d int) {
12+
dfs1 = func(root *TreeNode, depth int) {
1313
if root == nil {
1414
return
1515
}
16-
if len(s) <= d {
16+
if len(s) <= depth {
1717
s = append(s, 0)
1818
}
19-
s[d] += root.Val
20-
dfs1(root.Left, d+1)
21-
dfs1(root.Right, d+1)
19+
s[depth] += root.Val
20+
dfs1(root.Left, depth+1)
21+
dfs1(root.Right, depth+1)
2222
}
2323
var dfs2 func(*TreeNode, int)
24-
dfs2 = func(root *TreeNode, d int) {
25-
if root == nil {
26-
return
27-
}
24+
dfs2 = func(root *TreeNode, depth int) {
2825
l, r := 0, 0
2926
if root.Left != nil {
3027
l = root.Left.Val
3128
}
3229
if root.Right != nil {
3330
r = root.Right.Val
3431
}
32+
sub := l + r
33+
depth++
3534
if root.Left != nil {
36-
root.Left.Val = s[d] - l - r
35+
root.Left.Val = s[depth] - sub
36+
dfs2(root.Left, depth)
3737
}
3838
if root.Right != nil {
39-
root.Right.Val = s[d] - l - r
39+
root.Right.Val = s[depth] - sub
40+
dfs2(root.Right, depth)
4041
}
41-
dfs2(root.Left, d+1)
42-
dfs2(root.Right, d+1)
4342
}
4443
dfs1(root, 0)
4544
root.Val = 0
46-
dfs2(root, 1)
45+
dfs2(root, 0)
4746
return root
4847
}

solution/2600-2699/2641.Cousins in Binary Tree II/Solution.java

+13-14
Original file line numberDiff line numberDiff line change
@@ -19,35 +19,34 @@ class Solution {
1919
public TreeNode replaceValueInTree(TreeNode root) {
2020
dfs1(root, 0);
2121
root.val = 0;
22-
dfs2(root, 1);
22+
dfs2(root, 0);
2323
return root;
2424
}
2525

26-
private void dfs1(TreeNode root, int d) {
26+
private void dfs1(TreeNode root, int depth) {
2727
if (root == null) {
2828
return;
2929
}
30-
if (s.size() <= d) {
30+
if (s.size() <= depth) {
3131
s.add(0);
3232
}
33-
s.set(d, s.get(d) + root.val);
34-
dfs1(root.left, d + 1);
35-
dfs1(root.right, d + 1);
33+
s.set(depth, s.get(depth) + root.val);
34+
dfs1(root.left, depth + 1);
35+
dfs1(root.right, depth + 1);
3636
}
3737

38-
private void dfs2(TreeNode root, int d) {
39-
if (root == null) {
40-
return;
41-
}
38+
private void dfs2(TreeNode root, int depth) {
4239
int l = root.left == null ? 0 : root.left.val;
4340
int r = root.right == null ? 0 : root.right.val;
41+
int sub = l + r;
42+
++depth;
4443
if (root.left != null) {
45-
root.left.val = s.get(d) - l - r;
44+
root.left.val = s.get(depth) - sub;
45+
dfs2(root.left, depth);
4646
}
4747
if (root.right != null) {
48-
root.right.val = s.get(d) - l - r;
48+
root.right.val = s.get(depth) - sub;
49+
dfs2(root.right, depth);
4950
}
50-
dfs2(root.left, d + 1);
51-
dfs2(root.right, d + 1);
5251
}
5352
}

solution/2600-2699/2641.Cousins in Binary Tree II/Solution.py

+13-14
Original file line numberDiff line numberDiff line change
@@ -6,30 +6,29 @@
66
# self.right = right
77
class Solution:
88
def replaceValueInTree(self, root: Optional[TreeNode]) -> Optional[TreeNode]:
9-
def dfs1(root, d):
9+
def dfs1(root: Optional[TreeNode], depth: int):
1010
if root is None:
1111
return
12-
if len(s) <= d:
12+
if len(s) <= depth:
1313
s.append(0)
14-
s[d] += root.val
15-
dfs1(root.left, d + 1)
16-
dfs1(root.right, d + 1)
14+
s[depth] += root.val
15+
dfs1(root.left, depth + 1)
16+
dfs1(root.right, depth + 1)
1717

18-
def dfs2(root, d):
19-
if root is None:
20-
return
21-
t = (root.left.val if root.left else 0) + (
18+
def dfs2(root: Optional[TreeNode], depth: int):
19+
sub = (root.left.val if root.left else 0) + (
2220
root.right.val if root.right else 0
2321
)
22+
depth += 1
2423
if root.left:
25-
root.left.val = s[d] - t
24+
root.left.val = s[depth] - sub
25+
dfs2(root.left, depth)
2626
if root.right:
27-
root.right.val = s[d] - t
28-
dfs2(root.left, d + 1)
29-
dfs2(root.right, d + 1)
27+
root.right.val = s[depth] - sub
28+
dfs2(root.right, depth)
3029

3130
s = []
3231
dfs1(root, 0)
3332
root.val = 0
34-
dfs2(root, 1)
33+
dfs2(root, 0)
3534
return root

solution/2600-2699/2641.Cousins in Binary Tree II/Solution.ts

+13-15
Original file line numberDiff line numberDiff line change
@@ -14,33 +14,31 @@
1414

1515
function replaceValueInTree(root: TreeNode | null): TreeNode | null {
1616
const s: number[] = [];
17-
const dfs1 = (root: TreeNode | null, d: number): void => {
17+
const dfs1 = (root: TreeNode | null, depth: number) => {
1818
if (!root) {
1919
return;
2020
}
21-
if (s.length <= d) {
21+
if (s.length <= depth) {
2222
s.push(0);
2323
}
24-
s[d] += root.val;
25-
dfs1(root.left, d + 1);
26-
dfs1(root.right, d + 1);
24+
s[depth] += root.val;
25+
dfs1(root.left, depth + 1);
26+
dfs1(root.right, depth + 1);
2727
};
28-
const dfs2 = (root: TreeNode | null, d: number): void => {
29-
if (!root) {
30-
return;
31-
}
32-
const t = (root.left?.val ?? 0) + (root.right?.val ?? 0);
28+
const dfs2 = (root: TreeNode | null, depth: number) => {
29+
const sub = (root.left?.val || 0) + (root.right?.val || 0);
30+
++depth;
3331
if (root.left) {
34-
root.left.val = s[d] - t;
32+
root.left.val = s[depth] - sub;
33+
dfs2(root.left, depth);
3534
}
3635
if (root.right) {
37-
root.right.val = s[d] - t;
36+
root.right.val = s[depth] - sub;
37+
dfs2(root.right, depth);
3838
}
39-
dfs2(root.left, d + 1);
40-
dfs2(root.right, d + 1);
4139
};
4240
dfs1(root, 0);
4341
root.val = 0;
44-
dfs2(root, 1);
42+
dfs2(root, 0);
4543
return root;
4644
}

solution/2600-2699/2641.Cousins in Binary Tree II/Solution2.cpp

+10-11
Original file line numberDiff line numberDiff line change
@@ -13,31 +13,30 @@ class Solution {
1313
public:
1414
TreeNode* replaceValueInTree(TreeNode* root) {
1515
root->val = 0;
16-
vector<TreeNode*> q;
17-
q.emplace_back(root);
16+
vector<TreeNode*> q = {root};
1817
while (!q.empty()) {
19-
vector<TreeNode*> p = q;
20-
q.clear();
18+
vector<TreeNode*> t;
2119
int s = 0;
22-
for (TreeNode* node : p) {
20+
for (TreeNode* node : q) {
2321
if (node->left) {
24-
q.emplace_back(node->left);
22+
t.emplace_back(node->left);
2523
s += node->left->val;
2624
}
2725
if (node->right) {
28-
q.emplace_back(node->right);
26+
t.emplace_back(node->right);
2927
s += node->right->val;
3028
}
3129
}
32-
for (TreeNode* node : p) {
33-
int t = (node->left ? node->left->val : 0) + (node->right ? node->right->val : 0);
30+
for (TreeNode* node : q) {
31+
int sub = (node->left ? node->left->val : 0) + (node->right ? node->right->val : 0);
3432
if (node->left) {
35-
node->left->val = s - t;
33+
node->left->val = s - sub;
3634
}
3735
if (node->right) {
38-
node->right->val = s - t;
36+
node->right->val = s - sub;
3937
}
4038
}
39+
q = move(t);
4140
}
4241
return root;
4342
}

solution/2600-2699/2641.Cousins in Binary Tree II/Solution2.go

+11-11
Original file line numberDiff line numberDiff line change
@@ -10,34 +10,34 @@ func replaceValueInTree(root *TreeNode) *TreeNode {
1010
root.Val = 0
1111
q := []*TreeNode{root}
1212
for len(q) > 0 {
13-
p := q
14-
q = []*TreeNode{}
13+
t := []*TreeNode{}
1514
s := 0
16-
for _, node := range p {
15+
for _, node := range q {
1716
if node.Left != nil {
18-
q = append(q, node.Left)
17+
t = append(t, node.Left)
1918
s += node.Left.Val
2019
}
2120
if node.Right != nil {
22-
q = append(q, node.Right)
21+
t = append(t, node.Right)
2322
s += node.Right.Val
2423
}
2524
}
26-
for _, node := range p {
27-
t := 0
25+
for _, node := range q {
26+
sub := 0
2827
if node.Left != nil {
29-
t += node.Left.Val
28+
sub += node.Left.Val
3029
}
3130
if node.Right != nil {
32-
t += node.Right.Val
31+
sub += node.Right.Val
3332
}
3433
if node.Left != nil {
35-
node.Left.Val = s - t
34+
node.Left.Val = s - sub
3635
}
3736
if node.Right != nil {
38-
node.Right.Val = s - t
37+
node.Right.Val = s - sub
3938
}
4039
}
40+
q = t
4141
}
4242
return root
4343
}

solution/2600-2699/2641.Cousins in Binary Tree II/Solution2.java

+9-9
Original file line numberDiff line numberDiff line change
@@ -18,29 +18,29 @@ public TreeNode replaceValueInTree(TreeNode root) {
1818
root.val = 0;
1919
List<TreeNode> q = List.of(root);
2020
while (!q.isEmpty()) {
21-
List<TreeNode> p = q;
22-
q = new ArrayList<>();
21+
List<TreeNode> t = new ArrayList<>();
2322
int s = 0;
24-
for (TreeNode node : p) {
23+
for (TreeNode node : q) {
2524
if (node.left != null) {
26-
q.add(node.left);
25+
t.add(node.left);
2726
s += node.left.val;
2827
}
2928
if (node.right != null) {
30-
q.add(node.right);
29+
t.add(node.right);
3130
s += node.right.val;
3231
}
3332
}
34-
for (TreeNode node : p) {
35-
int t = (node.left == null ? 0 : node.left.val)
33+
for (TreeNode node : q) {
34+
int sub = (node.left == null ? 0 : node.left.val)
3635
+ (node.right == null ? 0 : node.right.val);
3736
if (node.left != null) {
38-
node.left.val = s - t;
37+
node.left.val = s - sub;
3938
}
4039
if (node.right != null) {
41-
node.right.val = s - t;
40+
node.right.val = s - sub;
4241
}
4342
}
43+
q = t;
4444
}
4545
return root;
4646
}

0 commit comments

Comments
 (0)