-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLeetCode450.java
More file actions
75 lines (68 loc) · 2.09 KB
/
LeetCode450.java
File metadata and controls
75 lines (68 loc) · 2.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import util.TreeNode;
public class LeetCode450 {
public static void main(String[] args) {
// 输入:root = [5,3,6,2,4,null,7], key = 3
// 输出:[5,4,6,2,null,null,7]
System.out
.println(new Solution450().deleteNode(TreeNode.buildTree(new Integer[] { 5, 3, 6, 2, 4, null, 7 }), 3));
// 输入: root = [5,3,6,2,4,null,7], key = 0
// 输出: [5,3,6,2,4,null,7]
System.out
.println(new Solution450().deleteNode(TreeNode.buildTree(new Integer[] { 5,
3, 6, 2, 4, null, 7 }), 0));
// 输入: root = [], key = 0
// 输出: []
System.out
.println(new Solution450().deleteNode(TreeNode.buildTree(new Integer[] {}),
0));
}
}
class Solution450 {
public TreeNode deleteNode(TreeNode root, int key) {
if (root == null) {
return null;
}
if (root.val == key) {
return getNewRoot(root);
}
TreeNode cur = root;
TreeNode pre = null;
while (cur != null) {
if (cur.val > key) {
pre = cur;
cur = cur.left;
} else if (cur.val < key) {
pre = cur;
cur = cur.right;
} else {
break;
}
}
if (cur != null) {
if (pre.left == cur) {
pre.left = getNewRoot(cur);
} else {
pre.right = getNewRoot(cur);
}
}
return root;
}
public TreeNode findLeftMost(TreeNode currentNode) {
while (currentNode.left != null) {
currentNode = currentNode.left;
}
return currentNode;
}
public TreeNode getNewRoot(TreeNode root) {
if (root.left != null && root.right != null) {
findLeftMost(root.right).left = root.left;
return root.right;
} else {
if (root.right == null) {
return root.left;
} else {
return root.right;
}
}
}
}