Skip to content

Commit 85ea797

Browse files
committed
Add python Solution
1 parent 5f0f3fb commit 85ea797

File tree

2 files changed

+47
-0
lines changed

2 files changed

+47
-0
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
class Solution:
2+
def getAllElements(self, root1, root2) :
3+
def inorder(node, res):
4+
if node:
5+
inorder(node.left, res)
6+
res.append(node.val)
7+
inorder(node.right, res)
8+
res,inor1,inor2 = [],[],[]
9+
inorder(root1,inor1)
10+
inorder(root2,inor2)
11+
l1, r1 = 0, len(inor1)
12+
l2, r2 = 0, len(inor2)
13+
while True:
14+
if l1 == r1:
15+
res.extend(inor2[l2:])
16+
break
17+
if l2 == r2:
18+
res.extend(inor1[l1:])
19+
break
20+
if inor1[l1] < inor2[l2]:
21+
res.append(inor1[l1])
22+
l1 += 1
23+
else:
24+
res.append(inor2[l2])
25+
l2 += 1
26+
return res
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
class Solution(object):
2+
def searchInsert(self, nums, target):
3+
"""
4+
:type nums: List[int]
5+
:type target: int
6+
:rtype: int
7+
"""
8+
l = len(nums)
9+
i = 0
10+
j = l-1
11+
res = l
12+
while (i<=j):
13+
mid = (i+j)/2
14+
if target == nums[mid]:
15+
res = mid
16+
break
17+
elif target < nums[mid]:
18+
res = mid
19+
j = mid -1
20+
else: i = mid+1
21+
return res

0 commit comments

Comments
 (0)