各类模糊点总结 关于—前缀和

1. LeetCode 297. Serialize and DeSerialize Binary Tree

题目:Design an algorithm to serialize and deserialize a binary tree.

1
2
3
4
5
6
7
8
9
You may serialize the following tree:

1
/
2 3
/
4 5

as "[1,2,3,null,null,4,5]"

2. LeetCode 652. Find Duplicate Subtrees

题目: Given a binary tree, return all duplicate subtrees. For each kind of duplicate subtrees, you only need to return the root node of any oneof them.

Two trees are duplicate if they have the same structure with same node values.

//画recursion tree

Example 1:

1
2
3
4
5
6
7
    1
/
2 3
/ /
4 2 4
/
4

The following are two duplicate subtrees:

1
2
3
  2
/
4

and

1
4

Therefore, you need to return above trees’ root in the form of a list.

3. LeetCode 572. Subtree of Another Tree

Example 1:
Given tree s:

1
2
3
4
5
    3
/
4 5
/
1 2

Given tree t:

1
2
3
  4 
/
1 2

Return

true

, because t has the same structure and node values with a subtree of s.

Example 2:
Given tree s:

1
2
3
4
5
6
7
    3
/
4 5
/
1 2
/
0

Given tree t:

1
2
3
  4
/
1 2

Return

false

关于—前缀和

1.前缀和 + hashTable

1. LeetCode 560. Subarray Sum Equals K

Given an array of integers and an integer k, you need to find the total number of continuous subarrays whose sum equals to k.

Example 1:

1
2
Input:nums = [1,1,1], k = 2
Output: 2

Note:

  1. The length of the array is in range [1, 20,000].
  2. The range of numbers in the array is [-1000, 1000] and the range of the integer k is [-1e7, 1e7].

2. LeetCode 325. Maximum Size Subarray Sum Equals K

Given an array nums and a target value k, find the maximum length of a subarray that sums to k. If there isn’t one, return 0 instead.

Note:
The sum of the entire nums array is guaranteed to fit within the 32-bit signed integer range.

Example 1:

1
2
3
Input: nums = [1, -1, 5, -2, 3], k = 3
Output: 4
Explanation: The subarray [1, -1, 5, -2] sums to 3 and is the longest.

Example 2:

1
2
3
Input: nums = [-2, -1, 2, 1], k = 1
Output: 2
Explanation: The subarray [-1, 2] sums to 1 and is the longest.

Follow Up:
Can you do it in O(n) time?

.