-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path450. Delete Node in a BST.rb
61 lines (54 loc) · 1.3 KB
/
450. Delete Node in a BST.rb
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# Definition for a binary tree node.
# class TreeNode
# attr_accessor :val, :left, :right
# def initialize(val = 0, left = nil, right = nil)
# @val = val
# @left = left
# @right = right
# end
# end
# @param {TreeNode} root
# @param {Integer} key
# @return {TreeNode}
def delete_node(root, key)
if root == nil then return nil end
if key < root.val then root.left = delete_node(root.left,key) end
if key > root.val then root.right = delete_node(root.right,key) end
if root.val == key
if root.left == nil && root.right == nil
return nil
elsif root.left == nil
temp = root.right
root = nil
return temp
elsif (root.right == nil)
temp = root.left
root = nil
return temp
else
node = root.right
until node.left == nil
prev = node
node = node.left
end
end
end
return root
end
=begin
if root.left.val != nil
root_l = root.left
unless root_l.right == nil
root_l = root_l.right
end
root.val = root_l.val
root_l == root_l.left
else
root_r = root.right
unless root_r.left == nil
root_r = root_r.left
end
rot.val = root_r.val
root_r = root_r.right
end
=end