|
44 | 44 | 4 7
|
45 | 45 | */
|
46 | 46 | 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; |
69 | 62 | }
|
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; |
74 | 79 | }
|
75 |
| - return root; |
76 |
| - } |
77 | 80 |
|
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; |
81 | 86 | }
|
82 |
| - return node; |
83 | 87 | }
|
84 | 88 |
|
85 | 89 | }
|
0 commit comments