提交 91557e42 编写于 作者: L luzhipeng

199 and 209

上级 5ba05c23
## 题目地址
https://leetcode.com/problems/binary-tree-right-side-view/description/
## 题目描述
```
Given a binary tree, imagine yourself standing on the right side of it, return the values of the nodes you can see ordered from top to bottom.
Example:
Input: [1,2,3,null,5,null,4]
Output: [1, 3, 4]
Explanation:
1 <---
/ \
2 3 <---
\ \
5 4 <---
```
## 思路
> 这道题和 leetcode 102 号问题《102.binary-tree-level-order-traversal》很像
这道题可以借助`队列`实现,首先把 root 入队,然后入队一个特殊元素 Null(来表示每层的结束)。
然后就是 while(queue.length), 每次处理一个节点,都将其子节点(在这里是 left 和 right)放到队列中。
然后不断的出队, 如果出队的是 null,则表式这一层已经结束了,我们就继续 push 一个 null。
## 关键点解析
- 队列
- 队列中用 Null(一个特殊元素)来划分每层
- 树的基本操作- 遍历 - 层次遍历(BFS)
- 二叉树的右视图可以看作是层次遍历每次只取每一层的最右边的元素
## 代码
```js
/*
* @lc app=leetcode id=199 lang=javascript
*
* [199] Binary Tree Right Side View
*
* https://leetcode.com/problems/binary-tree-right-side-view/description/
*
* algorithms
* Medium (46.74%)
* Total Accepted: 156.1K
* Total Submissions: 332.3K
* Testcase Example: '[1,2,3,null,5,null,4]'
*
* Given a binary tree, imagine yourself standing on the right side of it,
* return the values of the nodes you can see ordered from top to bottom.
*
* Example:
*
*
* Input: [1,2,3,null,5,null,4]
* Output: [1, 3, 4]
* Explanation:
*
* ⁠ 1 <---
* ⁠/ \
* 2 3 <---
* ⁠\ \
* ⁠ 5 4 <---
*
*/
/**
* Definition for a binary tree node.
* function TreeNode(val) {
* this.val = val;
* this.left = this.right = null;
* }
*/
/**
* @param {TreeNode} root
* @return {number[]}
*/
var rightSideView = function(root) {
if (!root) return [];
const ret = [];
const queue = [root, null];
let levelNodes = [];
while (queue.length > 0) {
const node = queue.shift();
if (node !== null) {
levelNodes.push(node.val);
if (node.right) {
queue.push(node.right);
}
if (node.left) {
queue.push(node.left);
}
} else {
// 一层遍历已经结束
ret.push(levelNodes[0]);
if (queue.length > 0) {
queue.push(null);
}
levelNodes = [];
}
}
return ret;
};
```
## 扩展
假如题目变成求二叉树的左视图呢?
很简单我们只需要取 queue 的最后一个元素即可。 或者存的时候反着来也行
> 其实我们没必要存储 levelNodes,而是只存储每一层最右的元素,这样空间复杂度就不是 n 了, 就是 logn 了。
## 题目地址
https://leetcode.com/problems/minimum-size-subarray-sum/description/
## 题目描述
```
Given an array of n positive integers and a positive integer s, find the minimal length of a contiguous subarray of which the sum ≥ s. If there isn't one, return 0 instead.
Example:
Input: s = 7, nums = [2,3,1,2,4,3]
Output: 2
Explanation: the subarray [4,3] has the minimal length under the problem constraint.
Follow up:
If you have figured out the O(n) solution, try coding another solution of which the time complexity is O(n log n).
```
## 思路
用滑动窗口来记录序列, 每当滑动窗口中的 sum 超过 s, 就去更新最小值,并根据先进先出的原则更新滑动窗口,直至 sum 刚好小于 s
> 这道题目和 leetcode 3 号题目有点像,都可以用滑动窗口的思路来解决
## 关键点
- 滑动窗口简化操作
## 代码
```js
/*
* @lc app=leetcode id=209 lang=javascript
*
* [209] Minimum Size Subarray Sum
*
* https://leetcode.com/problems/minimum-size-subarray-sum/description/
*
* algorithms
* Medium (34.31%)
* Total Accepted: 166.9K
* Total Submissions: 484.9K
* Testcase Example: '7\n[2,3,1,2,4,3]'
*
* Given an array of n positive integers and a positive integer s, find the
* minimal length of a contiguous subarray of which the sum ≥ s. If there isn't
* one, return 0 instead.
*
* Example:
*
*
* Input: s = 7, nums = [2,3,1,2,4,3]
* Output: 2
* Explanation: the subarray [4,3] has the minimal length under the problem
* constraint.
*
* Follow up:
*
* If you have figured out the O(n) solution, try coding another solution of
* which the time complexity is O(n log n).
*
*/
/**
* @param {number} s
* @param {number[]} nums
* @return {number}
*/
var minSubArrayLen = function(s, nums) {
if (nums.length === 0) return 0;
const slideWindow = [];
let acc = 0;
let min = null;
for (let i = 0; i < nums.length + 1; i++) {
const num = nums[i];
while (acc >= s) {
if (min === null || slideWindow.length < min) {
min = slideWindow.length;
}
acc = acc - slideWindow.shift();
}
slideWindow.push(num);
acc = slideWindow.reduce((a, b) => a + b, 0);
}
return min || 0;
};
```
## 扩展
如果题目要求是 sum = s, 而不是 sum >= s 呢?
eg:
```js
var minSubArrayLen = function(s, nums) {
if (nums.length === 0) return 0;
const slideWindow = [];
let acc = 0;
let min = null;
for (let i = 0; i < nums.length + 1; i++) {
const num = nums[i];
while (acc > s) {
acc = acc - slideWindow.shift();
}
if (acc === s) {
if (min === null || slideWindow.length < min) {
min = slideWindow.length;
}
slideWindow.shift();
}
slideWindow.push(num);
acc = slideWindow.reduce((a, b) => a + b, 0);
}
return min || 0;
};
```
# 介绍
leetcode题解,记录自己的leecode解题之路。
leetcode 题解,记录自己的 leecode 解题之路。
## 传送门
### 简单难度
- [20. Valid Parentheses](https://github.com/azl397985856/leetcode/blob/master/validParentheses.md)
- [26.remove-duplicates-from-sorted-array](https://github.com/azl397985856/leetcode/blob/master/26.remove-duplicates-from-sorted-array.md)
- [206.reverse-linked-list](./206.reverse-linked-list.md)
......@@ -14,6 +17,7 @@ leetcode题解,记录自己的leecode解题之路。
- [349.intersection-of-two-arrays](./349.intersection-of-two-arrays.md)
### 中等难度
- [2. Add Two Numbers](https://github.com/azl397985856/leetcode/blob/master/addTwoNumbers.md)
- [3. Longest Substring Without Repeating Characters](https://github.com/azl397985856/leetcode/blob/master/longestSubstringWithoutRepeatingCharacters.md)
- [5. Longest Palindromic Substring](https://github.com/azl397985856/leetcode/blob/master/longestPalindromicSubstring.md)
......@@ -31,7 +35,10 @@ leetcode题解,记录自己的leecode解题之路。
- [445.add-two-numbers-ii](./445.add-two-numbers-ii.md)
- [877.stone-game](./877.stone-game.md)
- [279.perfect-squares](./279.perfect-squares.md)
- [199.binary-tree-right-side-view](./199.binary-tree-right-side-view.md)
- [209.minimum-size-subarray-sum](./209.minimum-size-subarray-sum.md)
### 困难难度
- [145.binary-tree-postorder-traversal](./145.binary-tree-postorder-traversal.md)
- [146.lru-cache](./146.lru-cache.md)
\ No newline at end of file
- [146.lru-cache](./146.lru-cache.md)
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册