1 class Solution:
 2 
 3     def insertIntoBST(self, root, val):
 4         """
 5         Time:  O(log(n))  [average case]
 6         Space: O(1)
 7         """
 8         new_node = TreeNode(val)
 9 
10         if not root:
11              return new_node
12 
13         curr = root
14         while True:
15             if curr.val > val:
16                 if not curr.left:
17                     curr.left = new_node
18                     break
19                 else:
20                     curr = curr.left
21             else:
22                 if not curr.right:
23                     curr.right = new_node
24                     break
25                 else:
26                     curr = curr.right
27         return root

 

posted on 2019-02-11 16:34  Sempron2800+  阅读(90)  评论(0编辑  收藏  举报