比较基础的二叉树排序树插入,写了个递归。

# Definition for a binary tree node.
# class TreeNode(object):
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None

class Solution(object):
    def insertIntoBST(self, root, val):
        """
        :type root: TreeNode
        :type val: int
        :rtype: TreeNode
        """
        if root is None:
            return TreeNode(val)
        
        if val < root.val:
            root.left = self.insertIntoBST(root.left, val)
        else:
            root.right = self.insertIntoBST(root.right, val)
            
        return root

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2021-09-27
  • 2022-12-23
  • 2022-12-23
  • 2021-10-07
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2021-11-28
  • 2022-01-27
  • 2021-05-27
  • 2021-07-23
  • 2022-01-13
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案