Skip to content

Commit 10c2cba

Browse files
authored
feat(ml): 91 和 ml
2 parents f6fcaf6 + 30c70b9 commit 10c2cba

15 files changed

+3119
-114
lines changed

problems/1.two-sum.md

+18-1
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ https://leetcode-cn.com/problems/two-sum
5353

5454
## 代码
5555

56-
- 语言支持:JS
56+
- 语言支持:JS, Go
5757

5858
```js
5959
/**
@@ -73,6 +73,23 @@ const twoSum = function (nums, target) {
7373
};
7474
```
7575

76+
Go Code:
77+
78+
```go
79+
func twoSum(nums []int, target int) []int {
80+
m := make(map[int]int)
81+
for i, _ := range nums {
82+
diff := target - nums[i]
83+
if j, ok := m[diff]; ok {
84+
return []int{i, j}
85+
} else {
86+
m[nums[i]] = i
87+
}
88+
}
89+
return []int{}
90+
}
91+
```
92+
7693
**复杂度分析**
7794

7895
- 时间复杂度:$O(N)$

problems/100.same-tree.md

+230
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,230 @@
1+
## 题目地址(100. 相同的树)
2+
3+
https://leetcode-cn.com/problems/same-tree/
4+
5+
## 题目描述
6+
7+
```
8+
给定两个二叉树,编写一个函数来检验它们是否相同。
9+
10+
如果两个树在结构上相同,并且节点具有相同的值,则认为它们是相同的。
11+
12+
示例 1:
13+
14+
输入: 1 1
15+
/ \ / \
16+
2 3 2 3
17+
18+
[1,2,3], [1,2,3]
19+
20+
输出: true
21+
示例 2:
22+
23+
输入: 1 1
24+
/ \
25+
2 2
26+
27+
[1,2], [1,null,2]
28+
29+
输出: false
30+
示例 3:
31+
32+
输入: 1 1
33+
/ \ / \
34+
2 1 1 2
35+
36+
[1,2,1], [1,1,2]
37+
38+
输出: false
39+
```
40+
41+
## 前置知识
42+
43+
- 递归
44+
- 层序遍历
45+
- 前中序确定一棵树
46+
47+
## 递归
48+
49+
### 思路
50+
51+
最简单的想法是递归,这里先介绍下递归三要素
52+
53+
- 递归出口,问题最简单的情况
54+
- 递归调用总是去尝试解决更小的问题,这样问题才会被收敛到最简单的情况
55+
- 递归调用的父问题和子问题没有交集
56+
57+
尝试用递归去解决相同的树
58+
59+
1. 分解为子问题,相同的树分解为左子是否相同,右子是否相同
60+
2. 递归出口: 当树高度为 1 时,判断递归出口
61+
62+
### 代码
63+
64+
- 语言支持:JS, Go, PHP
65+
66+
JS Code:
67+
68+
```js
69+
var isSameTree = function (p, q) {
70+
if (!p || !q) {
71+
return !p && !q;
72+
}
73+
return (
74+
p.val === q.val &&
75+
isSameTree(p.left, q.left) &&
76+
isSameTree(p.right, q.right)
77+
);
78+
};
79+
```
80+
81+
Go Code:
82+
83+
```go
84+
func isSameTree(p *TreeNode, q *TreeNode) bool {
85+
if p == nil || q == nil {
86+
return p==nil&&q==nil
87+
}
88+
return p.Val == q.Val && isSameTree(p.Left, q.Left) && isSameTree(p.Right, q.Right)
89+
}
90+
```
91+
92+
PHP Code:
93+
94+
```php
95+
class Solution
96+
{
97+
98+
/**
99+
* @param TreeNode $p
100+
* @param TreeNode $q
101+
* @return Boolean
102+
*/
103+
function isSameTree($p, $q)
104+
{
105+
if (!$p || !$q) {
106+
return !$p && !$q;
107+
}
108+
return $p->val == $q->val &&
109+
$this->isSameTree($p->left, $q->left) &&
110+
$this->isSameTree($p->right, $q->right);
111+
}
112+
}
113+
```
114+
115+
**复杂度分析**
116+
117+
- 时间复杂度:$O(N)$,其中 N 为树的节点数。
118+
- 空间复杂度:$O(h)$,其中 h 为树的高度。
119+
120+
## 层序遍历
121+
122+
### 思路
123+
124+
判断两棵树是否相同,只需要判断树的整个结构相同, 判断树的结构是否相同,只需要判断树的每层内容是否相同。
125+
126+
### 代码
127+
128+
- 语言支持:JS
129+
130+
JS Code:
131+
132+
```js
133+
var isSameTree = function (p, q) {
134+
let curLevelA = [p];
135+
let curLevelB = [q];
136+
137+
while (curLevelA.length && curLevelB.length) {
138+
let nextLevelA = [];
139+
let nextLevelB = [];
140+
const isOK = isSameCurLevel(curLevelA, curLevelB, nextLevelA, nextLevelB);
141+
if (isOK) {
142+
curLevelA = nextLevelA;
143+
curLevelB = nextLevelB;
144+
} else {
145+
return false;
146+
}
147+
}
148+
149+
return true;
150+
};
151+
152+
function isSameCurLevel(curLevelA, curLevelB, nextLevelA, nextLevelB) {
153+
if (curLevelA.length !== curLevelB.length) {
154+
return false;
155+
}
156+
for (let i = 0; i < curLevelA.length; i++) {
157+
if (!isSameNode(curLevelA[i], curLevelB[i])) {
158+
return false;
159+
}
160+
curLevelA[i] && nextLevelA.push(curLevelA[i].left, curLevelA[i].right);
161+
curLevelB[i] && nextLevelB.push(curLevelB[i].left, curLevelB[i].right);
162+
}
163+
return true;
164+
}
165+
166+
function isSameNode(nodeA, nodeB) {
167+
if (!nodeA || !nodeB) {
168+
return nodeA === nodeB;
169+
}
170+
return nodeA.val === nodeB.val;
171+
// return nodeA === nodeB || (nodeA && nodeB && nodeA.val === nodeB.val);
172+
}
173+
```
174+
175+
**复杂度分析**
176+
177+
- 时间复杂度:$O(N)$,其中 N 为树的节点数。
178+
- 空间复杂度:$O(Q)$,其中 Q 为队列的长度最大值,在这里不会超过相邻两层的节点数的最大值。
179+
180+
## 前中序确定一棵树
181+
182+
### 思路
183+
184+
前序和中序的遍历结果确定一棵树,那么当两棵树前序遍历和中序遍历结果都相同,那是否说明两棵树也相同。
185+
186+
### 代码
187+
188+
- 语言支持:JS
189+
190+
JS Code:
191+
192+
```js
193+
var isSameTree = function (p, q) {
194+
const preorderP = preorder(p, []);
195+
const preorderQ = preorder(q, []);
196+
const inorderP = inorder(p, []);
197+
const inorderQ = inorder(q, []);
198+
return (
199+
preorderP.join("") === preorderQ.join("") &&
200+
inorderP.join("") === inorderQ.join("")
201+
);
202+
};
203+
204+
function preorder(root, arr) {
205+
if (root === null) {
206+
arr.push(" ");
207+
return arr;
208+
}
209+
arr.push(root.val);
210+
preorder(root.left, arr);
211+
preorder(root.right, arr);
212+
return arr;
213+
}
214+
215+
function inorder(root, arr) {
216+
if (root === null) {
217+
arr.push(" ");
218+
return arr;
219+
}
220+
inorder(root.left, arr);
221+
arr.push(root.val);
222+
inorder(root.right, arr);
223+
return arr;
224+
}
225+
```
226+
227+
**复杂度分析**
228+
229+
- 时间复杂度:$O(N)$,其中 N 为树的节点数。
230+
- 空间复杂度:使用了中序遍历的结果数组,因此空间复杂度为 $O(N)$,其中 N 为树的节点数。

problems/104.maximum-depth-of-binary-tree.md

+83-6
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ https://leetcode-cn.com/problems/maximum-depth-of-binary-tree/description/
2020
/ \
2121
15 7
2222
返回它的最大深度 3 。
23-
2423
```
2524

2625
## 前置知识
@@ -41,7 +40,8 @@ https://leetcode-cn.com/problems/maximum-depth-of-binary-tree/description/
4140
## 思路
4241

4342
由于树是一种递归的数据结构,因此用递归去解决的时候往往非常容易,这道题恰巧也是如此,
44-
用递归实现的代码如下:
43+
44+
- 用递归实现的代码如下:
4545

4646
```js
4747
var maxDepth = function (root) {
@@ -57,16 +57,14 @@ var maxDepth = function (root) {
5757
## 关键点解析
5858

5959
- 队列
60-
6160
- 队列中用 Null(一个特殊元素)来划分每层,或者在对每层进行迭代之前保存当前队列元素的个数(即当前层所含元素个数)
62-
6361
- 树的基本操作- 遍历 - 层次遍历(BFS)
6462

6563
## 代码
6664

67-
- 语言支持:JS,C++,Java,Python
65+
- 语言支持:JS,C++,Java,Python, Go, PHP
6866

69-
JavaScript Code:
67+
JS Code:
7068

7169
```js
7270
/*
@@ -216,6 +214,85 @@ class Solution:
216214
return depth
217215
```
218216

217+
Go Code:
218+
219+
```go
220+
/**
221+
* Definition for a binary tree node.
222+
* type TreeNode struct {
223+
* Val int
224+
* Left *TreeNode
225+
* Right *TreeNode
226+
* }
227+
*/
228+
// BFS
229+
func maxDepth(root *TreeNode) int {
230+
if root == nil {
231+
return 0
232+
}
233+
234+
depth := 1
235+
q := []*TreeNode{root, nil} // queue
236+
var node *TreeNode
237+
for len(q) > 0 {
238+
node, q = q[0], q[1:] // pop
239+
if node != nil {
240+
if node.Left != nil {
241+
q = append(q, node.Left)
242+
}
243+
if node.Right != nil {
244+
q = append(q, node.Right)
245+
}
246+
} else if len(q) > 0 { // 注意要判断队列是否只有一个 nil
247+
q = append(q, nil)
248+
depth++
249+
}
250+
}
251+
return depth
252+
}
253+
```
254+
255+
PHP Code:
256+
257+
```php
258+
/**
259+
* Definition for a binary tree node.
260+
* class TreeNode {
261+
* public $val = null;
262+
* public $left = null;
263+
* public $right = null;
264+
* function __construct($value) { $this->val = $value; }
265+
* }
266+
*/
267+
class Solution
268+
{
269+
270+
/**
271+
* @param TreeNode $root
272+
* @return Integer
273+
*/
274+
function maxDepth($root)
275+
{
276+
if (!$root) return 0;
277+
278+
$depth = 1;
279+
$arr = [$root, null];
280+
while ($arr) {
281+
/** @var TreeNode $node */
282+
$node = array_shift($arr);
283+
if ($node) {
284+
if ($node->left) array_push($arr, $node->left);
285+
if ($node->right) array_push($arr, $node->right);
286+
} elseif ($arr) {
287+
$depth++;
288+
array_push($arr, null);
289+
}
290+
}
291+
return $depth;
292+
}
293+
}
294+
```
295+
219296
**复杂度分析**
220297

221298
- 时间复杂度:$O(N)$

0 commit comments

Comments
 (0)