-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathsolution1.js
47 lines (41 loc) · 841 Bytes
/
solution1.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
/**
* 965. 单值二叉树
*
* https://leetcode-cn.com/problems/univalued-binary-tree/
*
* Easy
*
* 76ms 84.96%
* 34.2mb 6.98%
*
*/
const isUnivalTree = root => {
if (!root) {
return true
}
const queue = [root]
const val = root.val
while (queue.length) {
const max = queue.length
for (let i = 0; i < max; i++) {
const item = queue.pop()
if (item) {
if (!isEqual(item.val, val)) {
return false
}
if (item.left && !isEqual(item.left.val, val)) {
return false
}
if (item.right && !isEqual(item.right.val, val)) {
return false
}
item.left && queue.unshift(item.left)
item.right && queue.unshift(item.right)
}
}
}
return true
}
function isEqual (val1, val2) {
return val1 === val2
}