Skip to content

Commit a034ebc

Browse files
committed
feat: leetcode 1305
1 parent 0f01529 commit a034ebc

File tree

8 files changed

+138
-1
lines changed

8 files changed

+138
-1
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
<p align="center">
44
<!-- TOPICS COUNT START -->
5-
<img src="https://img.shields.io/badge/-进度:349-green" alt="进度:349">
5+
<img src="https://img.shields.io/badge/-进度:350-green" alt="进度:350">
66
<!-- TOPICS COUNT END -->
77
<a href="./assets/docs/TOPICS.md"><img src="https://img.shields.io/badge/-题库目录-blue" alt="题库目录"></a>
88
<a href="./assets/docs/CATEGORIES.md"><img src="https://img.shields.io/badge/-题库分类-red" alt="题库分类"></a>

assets/data/categories.json

+12
Original file line numberDiff line numberDiff line change
@@ -2450,6 +2450,12 @@
24502450
"path": "./problemset/number-of-enclaves/README.md",
24512451
"difficulty": "中等"
24522452
},
2453+
{
2454+
"id": "1305",
2455+
"title": "两颗二叉搜索树中的所有元素",
2456+
"path": "./problemset/all-elements-in-two-binary-search-trees/README.md",
2457+
"difficulty": "中等"
2458+
},
24532459
{
24542460
"id": "1765",
24552461
"title": "地图中的最高点",
@@ -2816,6 +2822,12 @@
28162822
"title": "两数之和 IV - 输入 BST",
28172823
"path": "./problemset/two-sum-iv-input-is-a-bst/README.md",
28182824
"difficulty": "简单"
2825+
},
2826+
{
2827+
"id": "1305",
2828+
"title": "两颗二叉搜索树中的所有元素",
2829+
"path": "./problemset/all-elements-in-two-binary-search-trees/README.md",
2830+
"difficulty": "中等"
28192831
}
28202832
]
28212833
},

assets/data/topics.json

+10
Original file line numberDiff line numberDiff line change
@@ -3179,6 +3179,16 @@
31793179
"url": "https://leetcode-cn.com/problems/maximum-number-of-balloons/",
31803180
"path": "./problemset/maximum-number-of-balloons/README.md"
31813181
},
3182+
{
3183+
"id": "1305",
3184+
"title": {
3185+
"cn": "两颗二叉搜索树中的所有元素",
3186+
"en": "all-elements-in-two-binary-search-trees"
3187+
},
3188+
"difficulty": "中等",
3189+
"url": "https://leetcode-cn.com/problems/all-elements-in-two-binary-search-trees/",
3190+
"path": "./problemset/all-elements-in-two-binary-search-trees/README.md"
3191+
},
31823192
{
31833193
"id": "1342",
31843194
"title": {

assets/docs/CATEGORIES.md

+2
Original file line numberDiff line numberDiff line change
@@ -462,6 +462,7 @@
462462
| 653. [两数之和 IV - 输入 BST](../../problemset/two-sum-iv-input-is-a-bst/README.md) | 简单 |
463463
| 838. [推多米诺](../../problemset/push-dominoes/README.md) | 中等 |
464464
| 1020. [飞地的数量](../../problemset/number-of-enclaves/README.md) | 中等 |
465+
| 1305. [两颗二叉搜索树中的所有元素](../../problemset/all-elements-in-two-binary-search-trees/README.md) | 中等 |
465466
| 1765. [地图中的最高点](../../problemset/map-of-highest-peak/README.md) | 中等 |
466467
| 2039. [网络空闲的时刻](../../problemset/the-time-when-the-network-becomes-idle/README.md) | 中等 |
467468
| 2049. [统计最高分的节点数目](../../problemset/count-nodes-with-the-highest-score/README.md) | 中等 |
@@ -544,6 +545,7 @@
544545
| 589. [N 叉树的前序遍历](../../problemset/n-ary-tree-preorder-traversal/README.md) | 简单 |
545546
| 606. [根据二叉树创建字符串](../../problemset/construct-string-from-binary-tree/README.md) | 简单 |
546547
| 653. [两数之和 IV - 输入 BST](../../problemset/two-sum-iv-input-is-a-bst/README.md) | 简单 |
548+
| 1305. [两颗二叉搜索树中的所有元素](../../problemset/all-elements-in-two-binary-search-trees/README.md) | 中等 |
547549

548550
## 枚举
549551

assets/docs/TOPICS.md

+2
Original file line numberDiff line numberDiff line change
@@ -636,6 +636,8 @@
636636

637637
[1189. “气球” 的最大数量](../../problemset/maximum-number-of-balloons/README.md)
638638

639+
[1305. 两颗二叉搜索树中的所有元素](../../problemset/all-elements-in-two-binary-search-trees/README.md)
640+
639641
[1342. 将数字变成 0 的操作次数](../../problemset/number-of-steps-to-reduce-a-number-to-zero/README.md)
640642

641643
[1380. 矩阵中的幸运数](../../problemset/lucky-numbers-in-a-matrix/README.md)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
# 两颗二叉搜索树中的所有元素
2+
3+
> 难度:中等
4+
>
5+
> https://leetcode-cn.com/problems/all-elements-in-two-binary-search-trees/
6+
7+
## 题目
8+
9+
给你 `root1``root2` 这两棵二叉搜索树。请你返回一个列表,其中包含 **两棵树** 中的所有整数并按 **升序** 排序。
10+
11+
### 示例
12+
13+
#### 示例 1:
14+
15+
![q2-e1](https://user-images.githubusercontent.com/54696834/166128218-4dfe62f5-b52e-48f3-84c4-100820db59ff.png)
16+
17+
```
18+
输入:root1 = [2,1,4], root2 = [1,0,3]
19+
输出:[0,1,1,2,3,4]
20+
```
21+
22+
#### 示例 2:
23+
24+
![q2-e5-](https://user-images.githubusercontent.com/54696834/166128221-19759c30-7192-4854-ba7d-eb12855437d7.png)
25+
26+
```
27+
输入:root1 = [1,null,8], root2 = [8,1]
28+
输出:[1,1,8,8]
29+
```
30+
31+
## 解题
32+
33+
```ts
34+
/**
35+
* 中序遍历 + 归并
36+
* @desc 时间复杂度 O(M+N) 空间复杂度 O(M+N)
37+
* @param root1
38+
* @param root2
39+
* @returns
40+
*/
41+
export function getAllElements(root1: TreeNode | null, root2: TreeNode | null): number[] {
42+
const nums1: number[] = []
43+
const nums2: number[] = []
44+
45+
inorder(root1, nums1)
46+
inorder(root2, nums2)
47+
48+
const merged: number[] = []
49+
50+
while (nums1.length && nums2.length)
51+
merged.push(nums1[0] <= nums2[0] ? nums1.shift()! : nums2.shift()!)
52+
53+
merged.push(...nums1, ...nums2)
54+
return merged
55+
56+
function inorder(node: TreeNode | null, nums: number[]) {
57+
if (!node) return
58+
inorder(node.left, nums)
59+
nums.push(node.val)
60+
inorder(node.right, nums)
61+
}
62+
}
63+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { describe, expect, it } from 'vitest'
2+
import { getAllElements } from '.'
3+
import type { TreeNode } from '~/utils/treeNode'
4+
import { createTreeNode } from '~/utils/treeNode'
5+
6+
describe('两颗二叉搜索树中的所有元素', () => {
7+
testCase(getAllElements)
8+
})
9+
10+
function testCase(fn: (root1: TreeNode | null, root2: TreeNode | null) => number[]) {
11+
it.each([
12+
[[2, 1, 4], [1, 0, 3], [0, 1, 1, 2, 3, 4]],
13+
[[1, null, 8], [8, 1], [1, 1, 8, 8]],
14+
])('示例%#', (root1, root2, expected) => {
15+
expect(fn(createTreeNode(root1), createTreeNode(root2))).toStrictEqual(expected)
16+
})
17+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import type { TreeNode } from '~/utils/treeNode'
2+
3+
/**
4+
* 中序遍历 + 归并
5+
* @desc 时间复杂度 O(M+N) 空间复杂度 O(M+N)
6+
* @param root1
7+
* @param root2
8+
* @returns
9+
*/
10+
export function getAllElements(root1: TreeNode | null, root2: TreeNode | null): number[] {
11+
const nums1: number[] = []
12+
const nums2: number[] = []
13+
14+
inorder(root1, nums1)
15+
inorder(root2, nums2)
16+
17+
const merged: number[] = []
18+
19+
while (nums1.length && nums2.length)
20+
merged.push(nums1[0] <= nums2[0] ? nums1.shift()! : nums2.shift()!)
21+
22+
merged.push(...nums1, ...nums2)
23+
return merged
24+
25+
function inorder(node: TreeNode | null, nums: number[]) {
26+
if (!node) return
27+
inorder(node.left, nums)
28+
nums.push(node.val)
29+
inorder(node.right, nums)
30+
}
31+
}

0 commit comments

Comments
 (0)