Skip to content

Commit 619775e

Browse files
committed
finish 116,117,560
1 parent 86ff932 commit 619775e

3 files changed

+339
-0
lines changed
Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
# 116. Populating Next Right Pointers in Each Node
2+
3+
- Difficulty: Medium.
4+
- Related Topics: Tree, Depth-first Search.
5+
- Similar Questions: Populating Next Right Pointers in Each Node II, Binary Tree Right Side View.
6+
7+
## Problem
8+
9+
Given a binary tree
10+
11+
```
12+
struct TreeLinkNode {
13+
TreeLinkNode *left;
14+
TreeLinkNode *right;
15+
TreeLinkNode *next;
16+
}
17+
```
18+
19+
Populate each next pointer to point to its next right node. If there is no next right node, the next pointer should be set to ```NULL```.
20+
21+
Initially, all next pointers are set to ```NULL```.
22+
23+
**Note:**
24+
25+
26+
27+
- You may only use constant extra space.
28+
29+
- Recursive approach is fine, implicit stack space does not count as extra space for this problem.
30+
31+
- You may assume that it is a perfect binary tree (ie, all leaves are at the same level, and every parent has two children).
32+
33+
34+
**Example:**
35+
36+
Given the following perfect binary tree,
37+
38+
```
39+
1
40+
/ \
41+
2 3
42+
/ \ / \
43+
4 5 6 7
44+
```
45+
46+
After calling your function, the tree should look like:
47+
48+
```
49+
1 -> NULL
50+
/ \
51+
2 -> 3 -> NULL
52+
/ \ / \
53+
4->5->6->7 -> NULL
54+
```
55+
56+
57+
## Solution 1
58+
59+
```javascript
60+
/**
61+
* Definition for binary tree with next pointer.
62+
* function TreeLinkNode(val) {
63+
* this.val = val;
64+
* this.left = this.right = this.next = null;
65+
* }
66+
*/
67+
68+
/**
69+
* @param {TreeLinkNode} root
70+
* @return {void} Do not return anything, modify tree in-place instead.
71+
*/
72+
var connect = function(root) {
73+
var stack = [];
74+
var tmp = null;
75+
var node = null;
76+
var next = null;
77+
var level = 0;
78+
79+
if (root) stack.push([root, 0]);
80+
81+
while (stack.length) {
82+
tmp = stack.shift();
83+
node = tmp[0];
84+
level = tmp[1];
85+
86+
next = stack[0] && stack[0][1] === level ? stack[0][0] : null;
87+
88+
node.next = next;
89+
90+
if (node.left) stack.push([node.left, level + 1]);
91+
if (node.right) stack.push([node.right, level + 1]);
92+
}
93+
};
94+
```
95+
96+
**Explain:**
97+
98+
nope.
99+
100+
**Complexity:**
101+
102+
* Time complexity : O(n).
103+
* Space complexity : O(n).
104+
105+
## Solution 2
106+
107+
```javascript
108+
/**
109+
* Definition for binary tree with next pointer.
110+
* function TreeLinkNode(val) {
111+
* this.val = val;
112+
* this.left = this.right = this.next = null;
113+
* }
114+
*/
115+
116+
/**
117+
* @param {TreeLinkNode} root
118+
* @return {void} Do not return anything, modify tree in-place instead.
119+
*/
120+
var connect = function(root) {
121+
if (!root) return;
122+
var now = root;
123+
var cur = null;
124+
while (now.left) {
125+
cur = now;
126+
while (cur) {
127+
cur.left.next = cur.right;
128+
if (cur.next) cur.right.next = cur.next.left;
129+
cur = cur.next;
130+
}
131+
now = now.left;
132+
}
133+
};
134+
```
135+
136+
**Explain:**
137+
138+
nope.
139+
140+
**Complexity:**
141+
142+
* Time complexity : O(n).
143+
* Space complexity : O(1).
Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
# 117. Populating Next Right Pointers in Each Node II
2+
3+
- Difficulty: Medium.
4+
- Related Topics: Tree, Depth-first Search.
5+
- Similar Questions: Populating Next Right Pointers in Each Node.
6+
7+
## Problem
8+
9+
Given a binary tree
10+
11+
```
12+
struct TreeLinkNode {
13+
TreeLinkNode *left;
14+
TreeLinkNode *right;
15+
TreeLinkNode *next;
16+
}
17+
```
18+
19+
Populate each next pointer to point to its next right node. If there is no next right node, the next pointer should be set to ```NULL```.
20+
21+
Initially, all next pointers are set to ```NULL```.
22+
23+
**Note:**
24+
25+
- You may only use constant extra space.
26+
- Recursive approach is fine, implicit stack space does not count as extra space for this problem.
27+
28+
**Example:**
29+
30+
Given the following binary tree,
31+
32+
```
33+
1
34+
/ \
35+
2 3
36+
/ \ \
37+
4 5 7
38+
```
39+
40+
After calling your function, the tree should look like:
41+
42+
```
43+
1 -> NULL
44+
/ \
45+
2 -> 3 -> NULL
46+
/ \ \
47+
4-> 5 -> 7 -> NULL
48+
```
49+
50+
## Solution 1
51+
52+
```javascript
53+
/**
54+
* Definition for binary tree with next pointer.
55+
* function TreeLinkNode(val) {
56+
* this.val = val;
57+
* this.left = this.right = this.next = null;
58+
* }
59+
*/
60+
61+
/**
62+
* @param {TreeLinkNode} root
63+
* @return {void} Do not return anything, modify tree in-place instead.
64+
*/
65+
var connect = function(root) {
66+
var stack = [];
67+
var tmp = null;
68+
var node = null;
69+
var next = null;
70+
var level = 0;
71+
72+
if (root) stack.push([root, 0]);
73+
74+
while (stack.length) {
75+
tmp = stack.shift();
76+
node = tmp[0];
77+
level = tmp[1];
78+
79+
next = stack[0] && stack[0][1] === level ? stack[0][0] : null;
80+
81+
node.next = next;
82+
83+
if (node.left) stack.push([node.left, level + 1]);
84+
if (node.right) stack.push([node.right, level + 1]);
85+
}
86+
};
87+
```
88+
89+
**Explain:**
90+
91+
nope.
92+
93+
**Complexity:**
94+
95+
* Time complexity : O(n).
96+
* Space complexity : O(n).
97+
98+
## Solution 2
99+
100+
```javascript
101+
/**
102+
* Definition for binary tree with next pointer.
103+
* function TreeLinkNode(val) {
104+
* this.val = val;
105+
* this.left = this.right = this.next = null;
106+
* }
107+
*/
108+
109+
/**
110+
* @param {TreeLinkNode} root
111+
* @return {void} Do not return anything, modify tree in-place instead.
112+
*/
113+
var connect = function(root) {
114+
var now = root;
115+
var cur = null;
116+
var tmp = null;
117+
var last = null;
118+
while (now) {
119+
tmp = new TreeLinkNode(0);
120+
last = tmp;
121+
cur = now;
122+
while (cur) {
123+
if (cur.left) { last.next = cur.left; last = last.next; }
124+
if (cur.right) { last.next = cur.right; last = last.next; }
125+
cur = cur.next;
126+
}
127+
now = tmp.next;
128+
}
129+
};
130+
```
131+
132+
**Explain:**
133+
134+
nope.
135+
136+
**Complexity:**
137+
138+
* Time complexity : O(n).
139+
* Space complexity : O(1).

501-600/560. Subarray Sum Equals K.md

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
# 560. Subarray Sum Equals K
2+
3+
- Difficulty: Medium.
4+
- Related Topics: Array, Hash Table.
5+
- Similar Questions: Two Sum, Continuous Subarray Sum, Subarray Product Less Than K, Find Pivot Index.
6+
7+
## Problem
8+
9+
Given an array of integers and an integer **k**, you need to find the total number of continuous subarrays whose sum equals to **k**.
10+
11+
**Example 1:**
12+
```
13+
Input:nums = [1,1,1], k = 2
14+
Output: 2
15+
```
16+
17+
**Note:**
18+
19+
- The length of the array is in range [1, 20,000].
20+
- The range of numbers in the array is [-1000, 1000] and the range of the integer **k** is [-1e7, 1e7].
21+
22+
## Solution
23+
24+
```javascript
25+
/**
26+
* @param {number[]} nums
27+
* @param {number} k
28+
* @return {number}
29+
*/
30+
var subarraySum = function(nums, k) {
31+
var map = {};
32+
var len = nums.length;
33+
var sum = 0;
34+
var res = 0;
35+
36+
map[0] = 1;
37+
38+
for (var i = 0; i < len; i++) {
39+
sum += nums[i];
40+
res += (map[sum - k] || 0);
41+
map[sum] = (map[sum] || 0) + 1;
42+
}
43+
44+
return res;
45+
};
46+
```
47+
48+
**Explain:**
49+
50+
`i` 处,`map[key]` 表示 `0 ~ i` 中和为 `key` 的子数组数量。
51+
52+
`i` 处,`0 ~ i` 的和为 `sum`,此前和为 `k` 的子数组个数为 `map[sum - k]`
53+
54+
**Complexity:**
55+
56+
* Time complexity : O(n).
57+
* Space complexity : O(n).

0 commit comments

Comments
 (0)