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

Commit 0867cbf

Browse files
committed
refactor: 整理文件格式
1 parent f245eb5 commit 0867cbf

26 files changed

+35
-0
lines changed
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

算法/验证平衡二叉树.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
var isValidBST = function(root) {
2+
function helper(node, lower, upper) {
3+
// 遇到空节点 直接返回true
4+
if (!node) return true
5+
6+
var val = node.val
7+
// 如果当前的值比下边界还小 就失败
8+
if (lower !== null && val <= lower) return false
9+
// 如果当前的值比上边界还大 就失败
10+
if (upper !== null && val >= upper) return false
11+
12+
// 以此检测右子树 分别以自身到上一次的上边界 作为范围
13+
if (node.right && !helper(node.right, val, upper)) return false
14+
// 以此检测左子树 分别以下边界到自身的值 作为范围
15+
// 也就是说 左边的子树里不能有任何超出这个范围的值
16+
if (node.left && !helper(node.left, lower, val)) return false
17+
18+
return true
19+
}
20+
return helper(root, null, null)
21+
}
22+
23+
console.log(isValidBST({
24+
val: 5,
25+
left: {
26+
val: 1,
27+
right: {
28+
val: 6,
29+
left: {
30+
val: 2
31+
}
32+
}
33+
},
34+
})
35+
)

0 commit comments

Comments
 (0)