File tree Expand file tree Collapse file tree 1 file changed +34
-0
lines changed Expand file tree Collapse file tree 1 file changed +34
-0
lines changed Original file line number Diff line number Diff line change
1
+ /**
2
+ * TC : log (n)
3
+ *
4
+ * */
5
+ class Solution {
6
+ public TreeNode minimumVal (TreeNode root ) {
7
+ TreeNode curr = root ;
8
+ while (curr != null && curr .left != null ) {
9
+ curr = curr .left ;
10
+ }
11
+ return curr ;
12
+ }
13
+
14
+ public TreeNode deleteNode (TreeNode root , int key ) {
15
+ if (root == null ) return null ;
16
+
17
+ if (key > root .val ) {
18
+ root .right = deleteNode (root .right , key );
19
+ } else if (key < root .val ) {
20
+ root .left = deleteNode (root .left , key );
21
+ } else {
22
+ if (root .left == null ) {
23
+ return root .right ;
24
+ } else if (root .right == null ) {
25
+ return root .left ;
26
+ } else {
27
+ TreeNode minVal = minimumVal (root );
28
+ root .val = minVal .val ;
29
+ root .right = deleteNode (root .right , minVal .val );
30
+ }
31
+ }
32
+ return root ;
33
+ }
34
+ }
You can’t perform that action at this time.
0 commit comments