题解 | #插入二叉搜索树#
插入二叉搜索树
https://www.nowcoder.com/practice/4900db9ddfbd43a7a9841c6a408bacf2
# class TreeNode: # def __init__(self, x): # self.val = x # self.left = None # self.right = None # # 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 # # # @param root TreeNode类 # @param val int整型 # @return TreeNode类 # class Solution: def insertToBST(self , root: TreeNode, val: int) -> TreeNode: # write code here if val >= root.val: if root.right: self.insertToBST(root.right,val) else: root.right = TreeNode(val) else: if root.left: self.insertToBST(root.left,val) else: root.left = TreeNode(val) return root