Skip to content

Commit 19c2959

Browse files
iamAntimPalAntim-IWPIamShiwangi
committed
Update readme.md
Co-Authored-By: Antim-IWP <203163676+Antim-IWP@users.noreply.github.com> Co-Authored-By: Shiwangi Srivastava <174641070+IamShiwangi@users.noreply.github.com>
1 parent 37c7540 commit 19c2959

File tree

1 file changed

+0
-250
lines changed

1 file changed

+0
-250
lines changed

Solution/841. Keys and Rooms/readme.md

Lines changed: 0 additions & 250 deletions
Original file line numberDiff line numberDiff line change
@@ -106,256 +106,6 @@ class Solution:
106106
return root
107107
```
108108

109-
#### Java
110-
111-
```java
112-
/**
113-
* Definition for a binary tree node.
114-
* public class TreeNode {
115-
* int val;
116-
* TreeNode left;
117-
* TreeNode right;
118-
* TreeNode() {}
119-
* TreeNode(int val) { this.val = val; }
120-
* TreeNode(int val, TreeNode left, TreeNode right) {
121-
* this.val = val;
122-
* this.left = left;
123-
* this.right = right;
124-
* }
125-
* }
126-
*/
127-
class Solution {
128-
public TreeNode deleteNode(TreeNode root, int key) {
129-
if (root == null) {
130-
return null;
131-
}
132-
if (root.val > key) {
133-
root.left = deleteNode(root.left, key);
134-
return root;
135-
}
136-
if (root.val < key) {
137-
root.right = deleteNode(root.right, key);
138-
return root;
139-
}
140-
if (root.left == null) {
141-
return root.right;
142-
}
143-
if (root.right == null) {
144-
return root.left;
145-
}
146-
TreeNode node = root.right;
147-
while (node.left != null) {
148-
node = node.left;
149-
}
150-
node.left = root.left;
151-
root = root.right;
152-
return root;
153-
}
154-
}
155-
```
156-
157-
#### C++
158-
159-
```cpp
160-
/**
161-
* Definition for a binary tree node.
162-
* struct TreeNode {
163-
* int val;
164-
* TreeNode *left;
165-
* TreeNode *right;
166-
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
167-
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
168-
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
169-
* };
170-
*/
171-
class Solution {
172-
public:
173-
TreeNode* deleteNode(TreeNode* root, int key) {
174-
if (!root) return root;
175-
if (root->val > key) {
176-
root->left = deleteNode(root->left, key);
177-
return root;
178-
}
179-
if (root->val < key) {
180-
root->right = deleteNode(root->right, key);
181-
return root;
182-
}
183-
if (!root->left) return root->right;
184-
if (!root->right) return root->left;
185-
TreeNode* node = root->right;
186-
while (node->left) node = node->left;
187-
node->left = root->left;
188-
root = root->right;
189-
return root;
190-
}
191-
};
192-
```
193-
194-
#### Go
195-
196-
```go
197-
/**
198-
* Definition for a binary tree node.
199-
* type TreeNode struct {
200-
* Val int
201-
* Left *TreeNode
202-
* Right *TreeNode
203-
* }
204-
*/
205-
func deleteNode(root *TreeNode, key int) *TreeNode {
206-
if root == nil {
207-
return nil
208-
}
209-
if root.Val > key {
210-
root.Left = deleteNode(root.Left, key)
211-
return root
212-
}
213-
if root.Val < key {
214-
root.Right = deleteNode(root.Right, key)
215-
return root
216-
}
217-
if root.Left == nil {
218-
return root.Right
219-
}
220-
if root.Right == nil {
221-
return root.Left
222-
}
223-
node := root.Right
224-
for node.Left != nil {
225-
node = node.Left
226-
}
227-
node.Left = root.Left
228-
root = root.Right
229-
return root
230-
}
231-
```
232-
233-
#### TypeScript
234-
235-
```ts
236-
/**
237-
* Definition for a binary tree node.
238-
* class TreeNode {
239-
* val: number
240-
* left: TreeNode | null
241-
* right: TreeNode | null
242-
* constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
243-
* this.val = (val===undefined ? 0 : val)
244-
* this.left = (left===undefined ? null : left)
245-
* this.right = (right===undefined ? null : right)
246-
* }
247-
* }
248-
*/
249-
250-
function deleteNode(root: TreeNode | null, key: number): TreeNode | null {
251-
if (root == null) {
252-
return root;
253-
}
254-
const { val, left, right } = root;
255-
if (val > key) {
256-
root.left = deleteNode(left, key);
257-
} else if (val < key) {
258-
root.right = deleteNode(right, key);
259-
} else {
260-
if (left == null && right == null) {
261-
root = null;
262-
} else if (left == null || right == null) {
263-
root = left || right;
264-
} else {
265-
if (right.left == null) {
266-
right.left = left;
267-
root = right;
268-
} else {
269-
let minPreNode = right;
270-
while (minPreNode.left.left != null) {
271-
minPreNode = minPreNode.left;
272-
}
273-
const minVal = minPreNode.left.val;
274-
root.val = minVal;
275-
minPreNode.left = deleteNode(minPreNode.left, minVal);
276-
}
277-
}
278-
}
279-
return root;
280-
}
281-
```
282-
283-
#### Rust
284-
285-
```rust
286-
// Definition for a binary tree node.
287-
// #[derive(Debug, PartialEq, Eq)]
288-
// pub struct TreeNode {
289-
// pub val: i32,
290-
// pub left: Option<Rc<RefCell<TreeNode>>>,
291-
// pub right: Option<Rc<RefCell<TreeNode>>>,
292-
// }
293-
//
294-
// impl TreeNode {
295-
// #[inline]
296-
// pub fn new(val: i32) -> Self {
297-
// TreeNode {
298-
// val,
299-
// left: None,
300-
// right: None
301-
// }
302-
// }
303-
// }
304-
use std::cell::RefCell;
305-
use std::rc::Rc;
306-
impl Solution {
307-
fn dfs(root: &Option<Rc<RefCell<TreeNode>>>) -> i32 {
308-
let node = root.as_ref().unwrap().borrow();
309-
if node.left.is_none() {
310-
return node.val;
311-
}
312-
Self::dfs(&node.left)
313-
}
314-
315-
pub fn delete_node(
316-
mut root: Option<Rc<RefCell<TreeNode>>>,
317-
key: i32,
318-
) -> Option<Rc<RefCell<TreeNode>>> {
319-
if root.is_some() {
320-
let mut node = root.as_mut().unwrap().borrow_mut();
321-
match node.val.cmp(&key) {
322-
std::cmp::Ordering::Less => {
323-
node.right = Self::delete_node(node.right.take(), key);
324-
}
325-
std::cmp::Ordering::Greater => {
326-
node.left = Self::delete_node(node.left.take(), key);
327-
}
328-
std::cmp::Ordering::Equal => {
329-
match (node.left.is_some(), node.right.is_some()) {
330-
(false, false) => {
331-
return None;
332-
}
333-
(true, false) => {
334-
return node.left.take();
335-
}
336-
(false, true) => {
337-
return node.right.take();
338-
}
339-
(true, true) => {
340-
if node.right.as_ref().unwrap().borrow().left.is_none() {
341-
let mut r = node.right.take();
342-
r.as_mut().unwrap().borrow_mut().left = node.left.take();
343-
return r;
344-
} else {
345-
let val = Self::dfs(&node.right);
346-
node.val = val;
347-
node.right = Self::delete_node(node.right.take(), val);
348-
}
349-
}
350-
};
351-
}
352-
}
353-
}
354-
root
355-
}
356-
}
357-
```
358-
359109
<!-- tabs:end -->
360110

361111
<!-- solution:end -->

0 commit comments

Comments
 (0)