Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 867dc16

Browse files
committed
docs: 新增No98、No104题解
1 parent e4927b5 commit 867dc16

File tree

3 files changed

+122
-0
lines changed

3 files changed

+122
-0
lines changed

‎leetcode刷题/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
- [No.48 旋转图像](https://github.com/Mayandev/javascript_algorithm/blob/master/leetcode%E5%88%B7%E9%A2%98/note/No48_rotate.md)
1414
- [No.66 加一](https://github.com/Mayandev/javascript_algorithm/blob/master/leetcode%E5%88%B7%E9%A2%98/note/No66_plus-one.md)
1515
- [No.70 爬楼梯](https://github.com/Mayandev/javascript_algorithm/blob/master/leetcode%E5%88%B7%E9%A2%98/note/No70_climb-stairs.md)
16+
- [No.98 验证二叉搜索树](https://github.com/Mayandev/javascript_algorithm/blob/master/leetcode%E5%88%B7%E9%A2%98/note/No98_is-valid-BST.md)
17+
- [No.104 二叉树的最大深度](https://github.com/Mayandev/javascript_algorithm/blob/master/leetcode%E5%88%B7%E9%A2%98/note/No104_max-depth.md)
1618
- [No.122 买卖股票的最佳时机 II](https://github.com/Mayandev/javascript_algorithm/blob/master/leetcode%E5%88%B7%E9%A2%98/note/No122_max-profit.md)
1719
- [No.125 验证回文串](https://github.com/Mayandev/javascript_algorithm/blob/master/leetcode%E5%88%B7%E9%A2%98/note/No125_is-palindrome.md)
1820
- [No.136 只出现一次的数字](https://github.com/Mayandev/javascript_algorithm/blob/master/leetcode%E5%88%B7%E9%A2%98/note/No136_single-number.md)

‎leetcode刷题/note/No104_max-depth.md

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
# No.104 二叉树的最大深度
2+
3+
难度:`easy`
4+
5+
给定一个二叉树,找出其最大深度。
6+
7+
二叉树的深度为根节点到最远叶子节点的最长路径上的节点数。
8+
9+
说明: 叶子节点是指没有子节点的节点。
10+
11+
## 示例
12+
13+
示例:
14+
给定二叉树 [3,9,20,null,null,15,7],
15+
```
16+
3
17+
/ \
18+
9 20
19+
/ \
20+
15 7
21+
返回它的最大深度 3 。
22+
```
23+
24+
## 解题思路
25+
26+
使用递归的思路,分别先计算左右子树的高度,然后返回最大值即可。
27+
28+
代码如下:
29+
30+
```javascript
31+
/**
32+
* Definition for a binary tree node.
33+
* function TreeNode(val) {
34+
* this.val = val;
35+
* this.left = this.right = null;
36+
* }
37+
*/
38+
/**
39+
* @param {TreeNode} root
40+
* @return {number}
41+
*/
42+
var maxDepth = function(root) {
43+
if(!root) return 0
44+
let left = maxDepth(root.left)
45+
let right = maxDepth(root.right)
46+
return Math.max(left,right) + 1
47+
};
48+
```
49+
50+
**解题思路二:**
51+
52+
如果使用迭代的方法,需要借助额外的栈空间。
53+
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
# No.98 验证二叉搜索树
2+
3+
难度: `middle`
4+
5+
给定一个二叉树,判断其是否是一个有效的二叉搜索树。
6+
7+
假设一个二叉搜索树具有如下特征:
8+
9+
- 节点的左子树只包含小于当前节点的数。
10+
- 节点的右子树只包含大于当前节点的数。
11+
- 所有左子树和右子树自身必须也是二叉搜索树。
12+
13+
## 示例
14+
15+
示例 1:
16+
```
17+
输入:
18+
2
19+
/ \
20+
1 3
21+
输出: true
22+
```
23+
示例 2:
24+
```
25+
输入:
26+
5
27+
/ \
28+
1 4
29+
/ \
30+
3 6
31+
输出: false
32+
解释: 输入为: [5,1,4,null,null,3,6]。
33+
根节点的值为 5 ,但是其右子节点值为 4 。
34+
```
35+
36+
## 解题思路
37+
38+
代码:
39+
40+
```javascript
41+
/**
42+
* Definition for a binary tree node.
43+
* function TreeNode(val) {
44+
* this.val = val;
45+
* this.left = this.right = null;
46+
* }
47+
*/
48+
/**
49+
* @param {TreeNode} root
50+
* @return {boolean}
51+
*/
52+
var isValidBST = function(root) {
53+
return helper(root, null, null);
54+
};
55+
56+
var helper = function(node, lower, upper) {
57+
if (node == null) return true;
58+
59+
let val = node.val;
60+
if (lower != null && val <= lower) return false;
61+
if (upper != null && val >= upper) return false;
62+
63+
if (! helper(node.right, val, upper)) return false;
64+
if (! helper(node.left, lower, val)) return false;
65+
return true;
66+
}
67+
```

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /