Skip to content

Commit ebf05b0

Browse files
committed
[E:11/529, M:13/965, H:0/384] add No: 450 Delete Node in a BST
1 parent 5119598 commit ebf05b0

File tree

10 files changed

+445
-11
lines changed

10 files changed

+445
-11
lines changed

leet/golang.go

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -85,16 +85,6 @@ func main() {
8585
},
8686
want: true,
8787
},
88-
{
89-
name: "test-[[1,3],[3,0,1],[2],[0]]",
90-
input: [][]int{
91-
{1, 3},
92-
{3, 0, 1},
93-
{2},
94-
{0},
95-
},
96-
want: false,
97-
},
9888
}
9989
10090
testLog := leet.NewTestLog(len(tests))
Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
package main
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"strings"
7+
"time"
8+
9+
"github.com/gladmo/leetcode/leet"
10+
"github.com/gladmo/leetcode/questions/serial/中等/450/golang/solution"
11+
"github.com/gladmo/leetcode/utils/tree"
12+
)
13+
14+
func main() {
15+
/*
16+
17+
[5,3,6,2,4,null,7]
18+
3
19+
20+
*/
21+
22+
tests := []struct {
23+
name string
24+
input1 *solution.TreeNode
25+
input2 int
26+
want []string
27+
}{
28+
{
29+
name: "test-[5,2,6,1,4,null,7,null,null,3]",
30+
input1: tree.CreateTree("[5,2,6,1,4,null,7,null,null,3]"),
31+
input2: 5,
32+
want: []string{
33+
"[6,3,7,2,4]",
34+
"[6,2,7,1,4,null,null,null,null,3]",
35+
},
36+
},
37+
{
38+
name: "test-[5,2,6,1,4,null,7,null,null,3]",
39+
input1: tree.CreateTree("[5,2,6,1,4,null,7,null,null,3]"),
40+
input2: 7,
41+
want: []string{
42+
"[5,2,6,1,4,null,null,null,null,3]",
43+
},
44+
},
45+
{
46+
name: "test-[5,2,6,1,4,null,7,null,null,3]",
47+
input1: tree.CreateTree("[5,2,6,1,4,null,7,null,null,3]"),
48+
input2: 6,
49+
want: []string{
50+
"[5,2,7,1,4,null,null,null,null,3]",
51+
},
52+
},
53+
{
54+
name: "test-[5,2,6,1,4,null,7,null,null,3]",
55+
input1: tree.CreateTree("[5,2,6,1,4,null,7,null,null,3]"),
56+
input2: 2,
57+
want: []string{
58+
"[5,3,6,1,4,null,7]",
59+
},
60+
},
61+
{
62+
name: "test-[5,3,6,2,4,null,7]",
63+
input1: tree.CreateTree("[5,3,6,2,4,null,7]"),
64+
input2: 3,
65+
want: []string{
66+
"[5,4,6,2,null,null,7]",
67+
"[5,2,6,null,4,null,7]",
68+
},
69+
},
70+
{
71+
name: "test-[5,3,6,2,4,null,7]",
72+
input1: tree.CreateTree("[5,3,6,2,4,null,7]"),
73+
input2: 0,
74+
want: []string{
75+
"[5,3,6,2,4,null,7]",
76+
},
77+
},
78+
{
79+
name: "test-[0]",
80+
input1: tree.CreateTree("[0]"),
81+
input2: 0,
82+
want: []string{
83+
"[]",
84+
},
85+
},
86+
{
87+
name: "test-[]",
88+
input1: tree.CreateTree("[]"),
89+
input2: 0,
90+
want: []string{
91+
"[]",
92+
},
93+
},
94+
}
95+
96+
testLog := leet.NewTestLog(len(tests))
97+
defer testLog.Render()
98+
99+
timeoutDuration := time.Second * 2
100+
101+
for idx, test := range tests {
102+
// 超时检测
103+
timeout := leet.Timeout(timeoutDuration, func(ctx context.Context, cancel context.CancelFunc) {
104+
105+
cancel()
106+
})
107+
108+
if timeout {
109+
testLog.Fail(idx+1, test.name, "timeout")
110+
continue
111+
}
112+
113+
got := solution.Export(test.input1, test.input2)
114+
if !strings.Contains(strings.Join(test.want, ","), got.String()) {
115+
testLog.Fail(idx+1, test.name, fmt.Sprintf("want: %v, got %v.", test.want, got))
116+
continue
117+
}
118+
119+
testLog.Pass(idx+1, test.name)
120+
}
121+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package solution
2+
3+
// type TreeNode struct {
4+
// Val int
5+
// Left *TreeNode
6+
// Right *TreeNode
7+
// }
8+
9+
type TreeNode = tree.Node
10+
11+
func Export(root *TreeNode, key int) *TreeNode {
12+
return deleteNode(root, key)
13+
}
14+
15+
/****************************************************/
16+
/******** 以下为 Leetcode 示例部分(提交PR请还原) *******/
17+
/******** 使用 (./leetcode clear) 初始化所有问题 *******/
18+
/****************************************************/
19+
20+
/**
21+
* Definition for a binary tree node.
22+
* type TreeNode struct {
23+
* Val int
24+
* Left *TreeNode
25+
* Right *TreeNode
26+
* }
27+
*/
28+
func deleteNode(root *TreeNode, key int) *TreeNode {
29+
30+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package solution
2+
3+
// type TreeNode struct {
4+
// Val int
5+
// Left *TreeNode
6+
// Right *TreeNode
7+
// }
8+
9+
type TreeNode = tree.Node
10+
11+
func Export(root *TreeNode, key int) *TreeNode {
12+
return deleteNode(root, key)
13+
}
14+
15+
/****************************************************/
16+
/******** 以下为 Leetcode 示例部分(提交PR请还原) *******/
17+
/******** 使用 (./leetcode clear) 初始化所有问题 *******/
18+
/****************************************************/
19+
20+
/**
21+
* Definition for a binary tree node.
22+
* type TreeNode struct {
23+
* Val int
24+
* Left *TreeNode
25+
* Right *TreeNode
26+
* }
27+
*/
28+
func deleteNode(root *TreeNode, key int) *TreeNode {
29+
30+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
## [删除二叉搜索树中的节点](https://leetcode-cn.com/problems/delete-node-in-a-bst/)
2+
3+
给定一个二叉搜索树的根节点 **root** 和一个值 **key**,删除二叉搜索树中的 **key **对应的节点,并保证二叉搜索树的性质不变。返回二叉搜索树(有可能被更新)的根节点的引用。
4+
5+
一般来说,删除节点可分为两个步骤:
6+
7+
1. 首先找到需要删除的节点;
8+
2. 如果找到了,删除它。
9+
10+
**说明:** 要求算法时间复杂度为 O(h),h 为树的高度。
11+
12+
**示例:**
13+
14+
`
15+
root = [5,3,6,2,4,null,7]
16+
key = 3
17+
18+
5
19+
/ \
20+
3 6
21+
/ \ \
22+
2 4 7
23+
24+
给定需要删除的节点值是 3,所以我们首先找到 3 这个节点,然后删除它。
25+
26+
一个正确的答案是 [5,4,6,2,null,null,7], 如下图所示。
27+
28+
5
29+
/ \
30+
4 6
31+
/ \
32+
2 7
33+
34+
另一个正确答案是 [5,2,6,null,4,null,7]
35+
36+
5
37+
/ \
38+
2 6
39+
\ \
40+
4 7
41+
`

questions/store.json

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.
Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
package main
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"strings"
7+
"time"
8+
9+
"github.com/gladmo/leetcode/leet"
10+
"github.com/gladmo/leetcode/questions/serial/中等/450/golang/solution"
11+
"github.com/gladmo/leetcode/utils/tree"
12+
)
13+
14+
func main() {
15+
/*
16+
17+
[5,3,6,2,4,null,7]
18+
3
19+
20+
*/
21+
22+
tests := []struct {
23+
name string
24+
input1 *solution.TreeNode
25+
input2 int
26+
want []string
27+
}{
28+
{
29+
name: "test-[5,2,6,1,4,null,7,null,null,3]",
30+
input1: tree.CreateTree("[5,2,6,1,4,null,7,null,null,3]"),
31+
input2: 5,
32+
want: []string{
33+
"[6,3,7,2,4]",
34+
"[6,2,7,1,4,null,null,null,null,3]",
35+
},
36+
},
37+
{
38+
name: "test-[5,2,6,1,4,null,7,null,null,3]",
39+
input1: tree.CreateTree("[5,2,6,1,4,null,7,null,null,3]"),
40+
input2: 7,
41+
want: []string{
42+
"[5,2,6,1,4,null,null,null,null,3]",
43+
},
44+
},
45+
{
46+
name: "test-[5,2,6,1,4,null,7,null,null,3]",
47+
input1: tree.CreateTree("[5,2,6,1,4,null,7,null,null,3]"),
48+
input2: 6,
49+
want: []string{
50+
"[5,2,7,1,4,null,null,null,null,3]",
51+
},
52+
},
53+
{
54+
name: "test-[5,2,6,1,4,null,7,null,null,3]",
55+
input1: tree.CreateTree("[5,2,6,1,4,null,7,null,null,3]"),
56+
input2: 2,
57+
want: []string{
58+
"[5,3,6,1,4,null,7]",
59+
},
60+
},
61+
{
62+
name: "test-[5,3,6,2,4,null,7]",
63+
input1: tree.CreateTree("[5,3,6,2,4,null,7]"),
64+
input2: 3,
65+
want: []string{
66+
"[5,4,6,2,null,null,7]",
67+
"[5,2,6,null,4,null,7]",
68+
},
69+
},
70+
{
71+
name: "test-[5,3,6,2,4,null,7]",
72+
input1: tree.CreateTree("[5,3,6,2,4,null,7]"),
73+
input2: 0,
74+
want: []string{
75+
"[5,3,6,2,4,null,7]",
76+
},
77+
},
78+
{
79+
name: "test-[0]",
80+
input1: tree.CreateTree("[0]"),
81+
input2: 0,
82+
want: []string{
83+
"[]",
84+
},
85+
},
86+
{
87+
name: "test-[]",
88+
input1: tree.CreateTree("[]"),
89+
input2: 0,
90+
want: []string{
91+
"[]",
92+
},
93+
},
94+
}
95+
96+
testLog := leet.NewTestLog(len(tests))
97+
defer testLog.Render()
98+
99+
timeoutDuration := time.Second * 2
100+
101+
for idx, test := range tests {
102+
// 超时检测
103+
timeout := leet.Timeout(timeoutDuration, func(ctx context.Context, cancel context.CancelFunc) {
104+
105+
cancel()
106+
})
107+
108+
if timeout {
109+
testLog.Fail(idx+1, test.name, "timeout")
110+
continue
111+
}
112+
113+
got := solution.Export(test.input1, test.input2)
114+
if !strings.Contains(strings.Join(test.want, ","), got.String()) {
115+
testLog.Fail(idx+1, test.name, fmt.Sprintf("want: %v, got %v.", test.want, got))
116+
continue
117+
}
118+
119+
testLog.Pass(idx+1, test.name)
120+
}
121+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package solution
2+
3+
// type TreeNode struct {
4+
// Val int
5+
// Left *TreeNode
6+
// Right *TreeNode
7+
// }
8+
9+
type TreeNode = tree.Node
10+
11+
func Export(root *TreeNode, key int) *TreeNode {
12+
return deleteNode(root, key)
13+
}
14+
15+
/****************************************************/
16+
/******** 以下为 Leetcode 示例部分(提交PR请还原) *******/
17+
/******** 使用 (./leetcode clear) 初始化所有问题 *******/
18+
/****************************************************/
19+
20+
/**
21+
* Definition for a binary tree node.
22+
* type TreeNode struct {
23+
* Val int
24+
* Left *TreeNode
25+
* Right *TreeNode
26+
* }
27+
*/
28+
func deleteNode(root *TreeNode, key int) *TreeNode {
29+
30+
}

0 commit comments

Comments
 (0)