二叉搜索树的实现以及相关操作(python)
class TreeNode(object):
def __init__(self,val):
self.val = val
self.left = None
self.right = None
class Solution(object):
def insert(self,root,val):
"""
:type root : TreeNode
:type x : int
:rtype root : TreeNode
"""
if not root:
root = TreeNode(val)
elif x < root.val:
root.left = self.insert(root.left,val)
else:
root.right = self.insert(root.right,val)
return root
def query(self,root,val):
"""
:type root : TreeNode
:type x : int
:rtype : bool
"""
if root == None:
return False
if root.val == val:
return True
elif val < root.val:
return self.query(root.left, val)
elif val > root.val:
return self.query(root.right, val)
def delete(self,root,val):
"""
:type root : TreeNode
:type x : int
:rtype root :TreeNode
"""
if root == None:
return
if val < root.val:
root.left = self.delNode(root.left, val)
elif val > root.val:
root.right = self.delNode(root.right, val)
else:
if root.left and root.right:
temp = self.findMin(root.right)
root.val = temp.val
root.right = self.delNode(root.right, temp.val)
elif root.right == None and root.left == None:
root = None
elif root.right == None:
root = root.left
elif root.left == None:
root = root.right
return root
def findMin(self,root):
"""
:type root : TreeNode
:rtype : int
"""
if root.left:
return self.findMin(root.left)
else:
return root
def findMax(self,root):
"""
:type root : TreeNode
:rtype : int
"""
if root.right:
return self.findMax(root.right)
else:
return root
def printTree(self,root):
"""
:type root : TreeNode
:rtype : None
"""
if not root:
return
self.printTree(root.left)
print(root.val,end = ' ')
self.printTree(root.right)
class Solution_2(object):
def lowestCommonAncestor(self, root, p, q):
"""
:type root: TreeNode
:type p: TreeNode
:type q: TreeNode
:rtype: TreeNode
"""
if root.val > p.val and root.val > q.val:
return self.lowestCommonAncestor(root.left,p,q)
elif root.val < p.val and root.val < q.val:
return self.lowestCommonAncestor(root.right,p,q)
else:
return root
def findMode(self, root):
"""
:type root: TreeNode
:rtype: List[int]
"""
def inorder(root,res = []):
if not root:
return
inorder(root.left,res)
res.append(root.val)
inorder(root.right,res)
return res
if not root:
return
temp = inorder(root)
dic = {}
res = []
for index,num in enumerate(temp):
if num not in dic:
dic[num] = 1
else:
dic[num] +=1
n = max(dic.values())
for index,num in dic.items():
if dic[index] == n:
res.append(index)
return res
def getMinimumDifference(self, root):
"""
:type root: TreeNode
:rtype: int
"""
def inorder(root,res = []):
if not root:
return
inorder(root.left)
res.append(root.val)
inorder(root.right)
return res
if not root:
return
temp = inorder(root)
temp_2 =[]
for i in range(1,len(temp)):
temp_2.append(abs(temp[i]-temp[i-1]))
return min(temp_2)
def convertBST(self, root):
"""
:type root: TreeNode
:rtype: TreeNode
"""
total = 0
node = root
stack = []
while stack or node is not None:
while node is not None:
stack.append(node)
node = node.right
node = stack.pop()
total += node.val
node.val = total
node = node.left
return root
def trimBST(self, root, L, R):
"""
:type root: TreeNode
:type L: int
:type R: int
:rtype: TreeNode
"""
pass
if __name__ == '__main__':
a = TreeNode(17)
b = TreeNode(5)
c = TreeNode(35)
d = TreeNode(2)
e = TreeNode(16)
f = TreeNode(29)
g = TreeNode(38)
h = TreeNode(33)
a.left = b
a.right = c
b.left = d
b.right = e
c.left = f
c.right = g
f.right = h
demo = Solution()
demo.printTree(a)
demo.insert(b,19)
demo.print(b)