-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsolution.py
More file actions
32 lines (28 loc) · 717 Bytes
/
solution.py
File metadata and controls
32 lines (28 loc) · 717 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# 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 not root:
return TreeNode(val)
p = root
pre = root
while p:
pre = p
if val < p.val:
p = p.left
else:
p = p.right
if val < pre.val:
pre.left = TreeNode(val)
else:
pre.right = TreeNode(val)
return root