Skip to content

Commit 388e553

Browse files
committed
Runtime 134 ms (Top 76.28%) | Memory 17.0 MB (Top 6.64%)
1 parent 307d512 commit 388e553

File tree

1 file changed

+10
-23
lines changed

1 file changed

+10
-23
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,11 @@
1-
# Definition for a binary tree node.
2-
# class TreeNode:
3-
# def __init__(self, val=0, left=None, right=None):
4-
# self.val = val
5-
# self.left = left
6-
# self.right = right
71
class Solution:
8-
def insertIntoBST(self, root: Optional[TreeNode], val: int) -> Optional[TreeNode]:
9-
if not root: return TreeNode(val)
10-
def search(root):
11-
if val>root.val:
12-
if root.right:
13-
search(root.right)
14-
else:
15-
root.right=TreeNode(val)
16-
return
17-
else:
18-
if root.left:
19-
search(root.left)
20-
else:
21-
root.left=TreeNode(val)
22-
return
23-
search(root)
24-
return root
2+
def insertIntoBST(self, root, val):
3+
if not root:
4+
return TreeNode(val)
5+
6+
if val<root.val:
7+
root.left = self.insertIntoBST(root.left, val)
8+
else:
9+
root.right = self.insertIntoBST(root.right, val)
10+
11+
return root

0 commit comments

Comments
 (0)