Skip to content

Commit b39f57f

Browse files
committed
[20180411] medium problems
1 parent 48cf141 commit b39f57f

File tree

2 files changed

+46
-0
lines changed

2 files changed

+46
-0
lines changed

โ€ŽREADME.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@
105105
| [0695](https://leetcode.com/problems/max-area-of-island/description/) | Max Area of Island | easy | python | |
106106
| [0696](https://leetcode.com/problems/count-binary-substrings/) | Count Binary Substrings | easy | [python](๐Ÿ™‚easy/0696.Count-Binary-Substrings/count_binary_substrings.py) | |
107107
| [0700](https://leetcode.com/problems/search-in-a-binary-search-tree/) | Search in a Binary Search Tree | easy | [python](๐Ÿ™‚easy/0700.Search-in-a-Binary-Search-Tree/search_in_a_binary_search_tree.py) | |
108+
| [0701](https://leetcode.com/problems/insert-into-a-binary-search-tree/) | Insert into a Binary Search Tree | medium | [python](๐Ÿค”medium/0701.Insert-into-a-Binary-Search-Tree/insert_into_a_binary_search_tree.py) | |
108109
| [0707](https://leetcode.com/problems/design-linked-list/) | Design Linked List | easy | golang | |
109110
| [0709](https://leetcode.com/problems/to-lower-case/description/) | To Lower Case | easy | golang | |
110111
| [0728](https://leetcode.com/problems/self-dividing-numbers/description/) | Self Dividing Numbers | easy | python | |
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# 701. Insert into a Binary Search Tree
2+
# https://leetcode.com/problems/insert-into-a-binary-search-tree/
3+
4+
5+
# Definition for a binary tree node.
6+
class TreeNode(object):
7+
def __init__(self, x):
8+
self.val = x
9+
self.left = None
10+
self.right = None
11+
12+
13+
class Solution(object):
14+
# val ์ด๋ผ๋Š” ๊ฐ’์ด ๋“ค์–ด์˜ฌ ๋•Œ TreeNode ์— ์œ„์น˜์‹œํ‚ค๋Š” ๋ฌธ์ œ๋‹ค.
15+
# ์ด ๋•Œ Tree๋Š” BinaryTree๋‹ค.
16+
def helper(self, node, val):
17+
18+
# leaf node ์— ์œ„์น˜์‹œํ‚ค๋„๋ก ํ–ˆ๋‹ค.
19+
# ๋ฐ”์ด๋„ˆ๋ฆฌ ํŠธ๋ฆฌ์ด๊ธฐ ๋•Œ๋ฌธ์— ๊ฐ’ ๋น„๊ตํ•˜๋ฉด์„œ ๊ฐ€์žฅ ํ•˜๋‹จ ๊นŒ์ง€ ๋‚ด๋ ค๊ฐ€๋ฉด ๋œ๋‹ค.
20+
if node is None:
21+
return
22+
23+
if node.val < val:
24+
if node.right is not None:
25+
self.helper(node.right, val)
26+
else:
27+
node.right = TreeNode(val)
28+
return
29+
else:
30+
if node.left is not None:
31+
self.helper(node.left, val)
32+
else:
33+
node.left = TreeNode(val)
34+
return
35+
36+
return
37+
38+
def insertIntoBST(self, root, val):
39+
"""
40+
:type root: TreeNode
41+
:type val: int
42+
:rtype: TreeNode
43+
"""
44+
self.helper(root, val)
45+
return root

0 commit comments

Comments
ย (0)