701. 二叉搜索树中的插入操作
class Solution:
def insertIntoBST(self, root: TreeNode, val: int) -> TreeNode:
if not root:
return TreeNode(val)
node = root
while node.left or node.right:
if node.val > val:
if node.left is None:
node.left = TreeNode(val)
return root
else:
node = node.left
else:
if node.right is None:
node.right = TreeNode(val)
return root
else:
node = node.right
if node.val > val:
node.left = TreeNode(val)
else:
node.right= TreeNode(val)
return root