-
Notifications
You must be signed in to change notification settings - Fork 2.4k
/
Copy path1609-even-odd-tree.js
67 lines (52 loc) · 1.81 KB
/
1609-even-odd-tree.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
/**
* Definition for a binary tree node.
* function TreeNode(val, left, right) {
* this.val = (val===undefined ? 0 : val)
* this.left = (left===undefined ? null : left)
* this.right = (right===undefined ? null : right)
* }
*/
/**
* Level Order Traversal | BFS
* Time O(n) | Space O(n)
* https://leetcode.com/problems/even-odd-tree
* @param {TreeNode} root
* @return {boolean}
*/
var isEvenOddTree = function(root) {
// helper function
const isStricklyIncreasingAndOdd = (arr) => {
for (let i = 0; i < arr.length; i++) {
const currElement = arr[i];
const nextElement = (arr[i + 1] !== undefined && arr[i + 1]) || Infinity;
if (currElement >= nextElement || currElement % 2 === 0) return false;
}
return true;
}
// helper function
const isStricklyDecreasingAndEven = (arr) => {
for (let i = 0; i < arr.length; i++) {
const currElement = arr[i];
const nextElement = (arr[i + 1] !== undefined && arr[i + 1]) || -Infinity;
if (currElement <= nextElement || currElement % 2 === 1) return false;
}
return true;
}
const q = new Queue();
q.enqueue([root, 0]);
while (!q.isEmpty()) {
const size = q.size();
const levelArr = [];
const level = q.front()[1];
for (let i = 0; i < size; i++) {
const element = q.dequeue();
const node = element[0];
levelArr.push(node.val);
node.left && q.enqueue([node.left, level + 1]);
node.right && q.enqueue([node.right, level + 1]);
}
if (level % 2 === 0 && !isStricklyIncreasingAndOdd(levelArr)) return false;
if (level % 2 === 1 && !isStricklyDecreasingAndEven(levelArr)) return false;
}
return true;
};