mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-10 20:40:39 +08:00
修改java版本解法的位置,适合图的解法放在第一个,第二个解法(难理解版本)放在其后,新增java版本的迭代法
This commit is contained in:
@ -268,6 +268,34 @@ public:
|
|||||||
|
|
||||||
|
|
||||||
## Java
|
## Java
|
||||||
|
```java
|
||||||
|
// 解法1(最好理解的版本)
|
||||||
|
class Solution {
|
||||||
|
public TreeNode deleteNode(TreeNode root, int key) {
|
||||||
|
if (root == null) return root;
|
||||||
|
if (root.val == key) {
|
||||||
|
if (root.left == null) {
|
||||||
|
return root.right;
|
||||||
|
} else if (root.right == null) {
|
||||||
|
return root.left;
|
||||||
|
} else {
|
||||||
|
TreeNode cur = root.right;
|
||||||
|
while (cur.left != null) {
|
||||||
|
cur = cur.left;
|
||||||
|
}
|
||||||
|
cur.left = root.left;
|
||||||
|
root = root.right;
|
||||||
|
return root;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (root.val > key) root.left = deleteNode(root.left, key);
|
||||||
|
if (root.val < key) root.right = deleteNode(root.right, key);
|
||||||
|
return root;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
```java
|
```java
|
||||||
class Solution {
|
class Solution {
|
||||||
public TreeNode deleteNode(TreeNode root, int key) {
|
public TreeNode deleteNode(TreeNode root, int key) {
|
||||||
@ -296,33 +324,57 @@ class Solution {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
递归法
|
||||||
```java
|
```java
|
||||||
// 解法2
|
|
||||||
class Solution {
|
class Solution {
|
||||||
public TreeNode deleteNode(TreeNode root, int key) {
|
public TreeNode deleteNode(TreeNode root, int key) {
|
||||||
if (root == null) return root;
|
if (root == null){
|
||||||
if (root.val == key) {
|
return null;
|
||||||
if (root.left == null) {
|
}
|
||||||
return root.right;
|
//寻找对应的对应的前面的节点,以及他的前一个节点
|
||||||
} else if (root.right == null) {
|
TreeNode cur = root;
|
||||||
return root.left;
|
TreeNode pre = null;
|
||||||
|
while (cur != null){
|
||||||
|
if (cur.val < key){
|
||||||
|
pre = cur;
|
||||||
|
cur = cur.right;
|
||||||
|
} else if (cur.val > key) {
|
||||||
|
pre = cur;
|
||||||
|
cur = cur.left;
|
||||||
}else {
|
}else {
|
||||||
TreeNode cur = root.right;
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (pre == null){
|
||||||
|
return deleteOneNode(cur);
|
||||||
|
}
|
||||||
|
if (pre.left !=null && pre.left.val == key){
|
||||||
|
pre.left = deleteOneNode(cur);
|
||||||
|
}
|
||||||
|
if (pre.right !=null && pre.right.val == key){
|
||||||
|
pre.right = deleteOneNode(cur);
|
||||||
|
}
|
||||||
|
return root;
|
||||||
|
}
|
||||||
|
|
||||||
|
public TreeNode deleteOneNode(TreeNode node){
|
||||||
|
if (node == null){
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
if (node.right == null){
|
||||||
|
return node.left;
|
||||||
|
}
|
||||||
|
TreeNode cur = node.right;
|
||||||
while (cur.left !=null){
|
while (cur.left !=null){
|
||||||
cur = cur.left;
|
cur = cur.left;
|
||||||
}
|
}
|
||||||
cur.left = root.left;
|
cur.left = node.left;
|
||||||
root = root.right;
|
return node.right;
|
||||||
return root;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (root.val > key) root.left = deleteNode(root.left, key);
|
|
||||||
if (root.val < key) root.right = deleteNode(root.right, key);
|
|
||||||
return root;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
## Python
|
## Python
|
||||||
递归法(版本一)
|
递归法(版本一)
|
||||||
```python
|
```python
|
||||||
|
Reference in New Issue
Block a user