美团一面手撕算法:从根节点到叶节点的路径数字之和
class Solution:
def sumNumbers(self , root: TreeNode) -> int:
# write code here
def dfs(root, preTotal):
if not root:
return 0
total = preTotal*10 +root.val
if not root.left and not root.right:
return total
else:
return dfs(root.left, total) + dfs(root.right, total)
return dfs(root, 0)
美团二面手撕算法:求数组的局部最大值
def local_maximum(li):
if li is None:
return
left = 0
right = len(li) - 1
while left < right:
mid = int((left + right) / 2)
if li[mid] > li[mid + 1]:
right = mid
else:
left = mid + 1
return li[left]
if __name__ == '__main__':
li = [1, 5, 3, 2, 1, 0]
result = local_maximum(li)
print(result)