Skip to content

Commit b90c66e

Browse files
refactor 450
1 parent 76336bc commit b90c66e

File tree

2 files changed

+39
-39
lines changed

2 files changed

+39
-39
lines changed

src/main/java/com/fishercoder/solutions/_450.java

Lines changed: 36 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -44,42 +44,46 @@
4444
4 7
4545
*/
4646
public class _450 {
47-
48-
/**credit: https://discuss.leetcode.com/topic/65792/recursive-easy-to-understand-java-solution
49-
* Steps:
50-
1. Recursively find the node that has the same value as the key, while setting the left/right nodes equal to the returned subtree
51-
2. Once the node is found, have to handle the below 4 cases
52-
a. node doesn't have left or right - return null
53-
b. node only has left subtree- return the left subtree
54-
c. node only has right subtree- return the right subtree
55-
d. node has both left and right - find the minimum value in the right subtree, set that value to the currently found node, then recursively delete the minimum value in the right subtree*/
56-
public TreeNode deleteNode(TreeNode root, int key) {
57-
if (root == null) {
58-
return root;
59-
}
60-
if (root.val > key) {
61-
root.left = deleteNode(root.left, key);
62-
} else if (root.val < key) {
63-
root.right = deleteNode(root.right, key);
64-
} else {
65-
if (root.left == null) {
66-
return root.right;
67-
} else if (root.right == null) {
68-
return root.left;
47+
public static class Solution1 {
48+
49+
/**
50+
* credit: https://discuss.leetcode.com/topic/65792/recursive-easy-to-understand-java-solution
51+
* Steps:
52+
* 1. Recursively find the node that has the same value as the key, while setting the left/right nodes equal to the returned subtree
53+
* 2. Once the node is found, have to handle the below 4 cases
54+
* a. node doesn't have left or right - return null
55+
* b. node only has left subtree- return the left subtree
56+
* c. node only has right subtree- return the right subtree
57+
* d. node has both left and right - find the minimum value in the right subtree, set that value to the currently found node, then recursively delete the minimum value in the right subtree
58+
*/
59+
public TreeNode deleteNode(TreeNode root, int key) {
60+
if (root == null) {
61+
return root;
6962
}
70-
71-
TreeNode minNode = getMin(root.right);
72-
root.val = minNode.val;
73-
root.right = deleteNode(root.right, root.val);
63+
if (root.val > key) {
64+
root.left = deleteNode(root.left, key);
65+
} else if (root.val < key) {
66+
root.right = deleteNode(root.right, key);
67+
} else {
68+
if (root.left == null) {
69+
return root.right;
70+
} else if (root.right == null) {
71+
return root.left;
72+
}
73+
74+
TreeNode minNode = getMin(root.right);
75+
root.val = minNode.val;
76+
root.right = deleteNode(root.right, root.val);
77+
}
78+
return root;
7479
}
75-
return root;
76-
}
7780

78-
private TreeNode getMin(TreeNode node) {
79-
while (node.left != null) {
80-
node = node.left;
81+
private TreeNode getMin(TreeNode node) {
82+
while (node.left != null) {
83+
node = node.left;
84+
}
85+
return node;
8186
}
82-
return node;
8387
}
8488

8589
}

src/test/java/com/fishercoder/_450Test.java

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,14 @@
77

88
import static org.junit.Assert.assertEquals;
99

10-
/**
11-
* Created by stevesun on 5/29/17.
12-
*/
1310
public class _450Test {
14-
private static _450 test;
11+
private static _450.Solution1 solution1;
1512
private static TreeNode expected;
16-
private static TreeNode actual;
1713
private static TreeNode root;
1814

1915
@BeforeClass
2016
public static void setup() {
21-
test = new _450();
17+
solution1 = new _450.Solution1();
2218
}
2319

2420
@Test
@@ -36,6 +32,6 @@ public void test1() {
3632
expected.left.left = new TreeNode(2);
3733
expected.right.right = new TreeNode(7);
3834

39-
assertEquals(expected, test.deleteNode(root, 3));
35+
assertEquals(expected, solution1.deleteNode(root, 3));
4036
}
4137
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy