-
-
Notifications
You must be signed in to change notification settings - Fork 298
/
Copy path701.py
42 lines (38 loc) · 1.42 KB
/
701.py
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
33
34
35
36
37
38
39
40
41
42
__________________________________________________________________________________________________
sample 104 ms submission
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution:
def insertIntoBST(self, root: TreeNode, val: int) -> TreeNode:
if not root:
return
if val > root.val:
if root.right == None:
root.right = TreeNode(val)
else:
self.insertIntoBST(root.right, val)
elif val < root.val:
if root.left == None:
root.left = TreeNode(val)
else:
self.insertIntoBST(root.left, val)
return root
__________________________________________________________________________________________________
sample 15252 kb submission
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution:
def insertIntoBST(self, root, val):
if(root == None): return TreeNode(val);
if(root.val < val): root.right = self.insertIntoBST(root.right, val);
else: root.left = self.insertIntoBST(root.left, val);
return(root)
__________________________________________________________________________________________________