mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2026-02-02 18:39:09 +08:00
first commit
This commit is contained in:
@@ -251,7 +251,34 @@ public:
|
||||
|
||||
|
||||
Java:
|
||||
```java
|
||||
class Solution {
|
||||
public TreeNode deleteNode(TreeNode root, int key) {
|
||||
root = delete(root,key);
|
||||
return root;
|
||||
}
|
||||
|
||||
private TreeNode delete(TreeNode root, int key) {
|
||||
if (root == null) return null;
|
||||
|
||||
if (root.val > key) {
|
||||
root.left = delete(root.left,key);
|
||||
} else if (root.val < key) {
|
||||
root.right = delete(root.right,key);
|
||||
} else {
|
||||
if (root.left == null) return root.right;
|
||||
if (root.right == null) return root.left;
|
||||
TreeNode tmp = root.right;
|
||||
while (tmp.left != null) {
|
||||
tmp = tmp.left;
|
||||
}
|
||||
root.val = tmp.val;
|
||||
root.right = delete(root.right,tmp.val);
|
||||
}
|
||||
return root;
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
Python:
|
||||
|
||||
@@ -265,4 +292,4 @@ Go:
|
||||
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)
|
||||
* B站视频:[代码随想录](https://space.bilibili.com/525438321)
|
||||
* 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ)
|
||||
<div align="center"><img src=../pics/公众号.png width=450 alt=> </img></div>
|
||||
<div align="center"><img src=../pics/公众号.png width=450 alt=> </img></div>
|
||||
Reference in New Issue
Block a user