Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 98afe08

Browse files
committed
docs: 新增No.101、No.102题解
1 parent 1042fe5 commit 98afe08

File tree

3 files changed

+126
-0
lines changed

3 files changed

+126
-0
lines changed

leetcode刷题/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
- [No.70 爬楼梯](note/No70_climb-stairs.md)
1818
- [No.88 合并两个有序数组](note/No88_merge.md)
1919
- [No.98 验证二叉搜索树](note/No98_is-valid-BST.md)
20+
- [No.101 对称二叉树](note/No101_is-symmetric.md)
21+
- [No.102 二叉树的层次遍历](note/No102_level-order.md)
2022
- [No.104 二叉树的最大深度](note/No104_max-depth.md)
2123
- [No.121 买卖股票的最佳时机](note/No121_max-profit.md)
2224
- [No.122 买卖股票的最佳时机 II](note/No122_max-profit.md)
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
# No.101 对称二叉树
2+
3+
难度:`easy`
4+
5+
6+
7+
给定一个二叉树,检查它是否是镜像对称的。
8+
9+
例如,二叉树 [1,2,2,3,4,4,3] 是对称的。
10+
```
11+
1
12+
/ \
13+
2 2
14+
/ \ / \
15+
3 4 4 3
16+
```
17+
但是下面这个 [1,2,2,null,3,null,3] 则不是镜像对称的:
18+
```
19+
1
20+
/ \
21+
2 2
22+
\ \
23+
3 3
24+
```
25+
说明:
26+
27+
如果你可以运用递归和迭代两种方法解决这个问题,会很加分。
28+
29+
## 解题思路
30+
31+
使用递归的思路,同时判断左右是否相等.
32+
33+
代码如下:
34+
35+
```javascript
36+
/**
37+
* Definition for a binary tree node.
38+
* function TreeNode(val) {
39+
* this.val = val;
40+
* this.left = this.right = null;
41+
* }
42+
*/
43+
/**
44+
* @param {TreeNode} root
45+
* @return {boolean}
46+
*/
47+
var isSymmetric = function(root) {
48+
return isMirror(root,root);
49+
};
50+
51+
var isMirror = function(l,r) {
52+
if (l == null && r == null) return true;
53+
if (l == null || r == null) return false;
54+
return (l.val == r.val)
55+
&& isMirror(l.right, r.left)
56+
&& isMirror(l.left, r.right);
57+
}
58+
```
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
# No.102 二叉树的层次遍历
2+
3+
难度:`middle`
4+
5+
给定一个二叉树,返回其按层次遍历的节点值。 (即逐层地,从左到右访问所有节点)。
6+
7+
例如:
8+
给定二叉树: [3,9,20,null,null,15,7],
9+
10+
```
11+
3
12+
/ \
13+
9 20
14+
/ \
15+
15 7
16+
```
17+
返回其层次遍历结果:
18+
19+
```
20+
[
21+
[3],
22+
[9,20],
23+
[15,7]
24+
]
25+
```
26+
27+
## 解题思路
28+
29+
使用队列的思路,宽度优先搜索,每次遍历都将节点入队列,并加入到输出的数组中。
30+
31+
代码如下:
32+
33+
```javascript
34+
/**
35+
* Definition for a binary tree node.
36+
* function TreeNode(val) {
37+
* this.val = val;
38+
* this.left = this.right = null;
39+
* }
40+
*/
41+
/**
42+
* @param {TreeNode} root
43+
* @return {number[][]}
44+
*/
45+
var levelOrder = function(root) {
46+
if (root == null) {
47+
return [];
48+
}
49+
let queue = [root];
50+
let res = [];
51+
while (queue.length) {
52+
let arr = [];
53+
let length = queue.length;
54+
for (let i = 0; i < length; i++) {
55+
let front = queue.shift();
56+
arr.push(front.val);
57+
if (front.left)
58+
queue.push(front.left);
59+
if (front.right)
60+
queue.push(front.right);
61+
}
62+
res.push(arr);
63+
}
64+
return res;
65+
};
66+
```

0 commit comments

Comments
 (0)