deno.land / x / masx200_leetcode_test@10.6.5 / delete-node-in-a-bst / index.ts

نووسراو ببینە
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
import { TreeNode } from "../binary-tree-inorder-traversal/TreeNode.ts";
export default function deleteNode( root: TreeNode | null, key: number,): TreeNode | null { if (!root) return null;
if (root.val > key) { root.left = deleteNode(root.left, key); return root; } if (root.val < key) { root.right = deleteNode(root.right, key); return root; } if (!root.left && !root.right) return null; if (!root.left) { return root.right; } if (!root.right) { return root.left; }
let right = root.right;
while (right.left) { right = right.left; } right.left = root.left; return root.right;}
masx200_leetcode_test

Version Info

Tagged at
a year ago