File tree Expand file tree Collapse file tree 1 file changed +32
-0
lines changed Expand file tree Collapse file tree 1 file changed +32
-0
lines changed Original file line number Diff line number Diff line change 1+ /**
2+ * Definition for a binary tree node.
3+ * function TreeNode(val) {
4+ * this.val = val;
5+ * this.left = this.right = null;
6+ * }
7+ */
8+ /**
9+ * @param {TreeNode } root
10+ * @param {number } key
11+ * @return {TreeNode }
12+ */
13+ const deleteNode = function ( root , key ) {
14+ if ( root == null ) return null
15+ if ( root . val > key ) {
16+ root . left = deleteNode ( root . left , key )
17+ } else if ( root . val < key ) {
18+ root . right = deleteNode ( root . right , key )
19+ } else {
20+ if ( root . left == null ) {
21+ return root . right
22+ }
23+ if ( root . right == null ) {
24+ return root . left
25+ }
26+ let rightSmallest = root . right
27+ while ( rightSmallest . left != null ) rightSmallest = rightSmallest . left
28+ rightSmallest . left = root . left
29+ return root . right
30+ }
31+ return root
32+ } ;
You can’t perform that action at this time.
0 commit comments