forked from yingl/LintCodeInPython
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathremove_node_in_binary_search_tree.py
More file actions
38 lines (36 loc) · 1.3 KB
/
remove_node_in_binary_search_tree.py
File metadata and controls
38 lines (36 loc) · 1.3 KB
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
# -*- coding: utf-8 -*-
class Solution:
"""
@param root: The root of the binary search tree.
@param value: Remove the node with given value.
@return: The root of the binary search tree after removal.
"""
def removeNode(self, root, value):
# write your code here
if not root:
return
if root.val == value:
new_root = self.rebuildTree(root.left, root.right)
# 不能简单的root等于new_root,因为是引用,必须修改值!!!
if new_root:
root.val = new_root.val
root.left, root.right = new_root.left, new_root.right
else:
root = None
elif root.val < value:
self.removeNode(root.right, value)
else:
self.removeNode(root.left, value)
return root
def rebuildTree(self, left, right): # 根据左右子树生成一棵新的树
if not left:
return right
if not right:
return left
'''
利用递归把left的左右子树生成一颗新树,然后left.left指向它,
left.right继续指向原来的子树不用变化。
'''
left.left = self.rebuildTree(left.left, left.right)
left.right = right
return left