S算法問題: Construct Binary Tree from Inorder and Postorder Traversal
問題描述:
Given inorder and postorder traversal of a tree, construct the binary tree.
Note:
You may assume that duplicates do not exist in the tree.
S算法問題: Construct Binary Tree from Preorder and Inorder Traversal
問題描述:
Given preorder and inorder traversal of a tree, construct the binary tree.
Note:
You may assume that duplicates do not exist in the tree.
S算法問題: Container With Most Water
問題描述:
Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai).
n vertical lines are drawn such that the two endpoints of line i is at (i, ai) and (i, 0).
Find two lines, which together with x-axis forms a container, such that the container contains the most water.
Note: You may not slant the container.
算法問題推薦解法來了...
From both sides to the center.
解法(Python)
代碼: |
class Solution:
def maxArea(self, height):
i, j, max_area = 0, len(height) - 1, 0
while i < j:
max_area = max(max_area, (j - i) * min(height[i], height[j]))
if height[i] < height[j]:
i += 1
else:
j -= 1
return max_area
|
S算法問題: Convert Sorted Array to Binary Search Tree
問題描述:
Given an array where elements are sorted in ascending order, convert it to a height balanced BST.
S算法問題: Convert Sorted List to Binary Search Tree
問題描述:
Given a singly linked list where elements are sorted in ascending order, convert it to a height balanced BST.