[英]Inserting a node in Binary search tree in python
def putValueInBST(root, val): # assuming the val = 7 which is max of all the existing node values in BST
if root is None:
root = Node(val)
elif val > root.data:
putValueInBST(root.right, val)
else:
putValueInBST(root.left, val)
def put_val_manually(r, val):
r.right.right = Node(val)
(當然在put_val_manually()
函數中,我是直接做的。)
完整代碼在這里: https : //i.ibb.co/yf2YTYy/code.png
嘗試返回節點
def putValueInBST(root, val): # assuming the val = 7 which is max of all the existing node values in BST
if root is None:
return Node(val)
elif val > root.data:
root.right=putValueInBST(root.right, val)
else:
root.left=putValueInBST(root.left, val)
root=putValueInBST(root,val)
此代碼有效
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.