-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy path1609-even-odd-tree.js
57 lines (50 loc) · 1.69 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
/**
* 1609. Even Odd Tree
* https://leetcode.com/problems/even-odd-tree/
* Difficulty: Medium
*
* A binary tree is named Even-Odd if it meets the following conditions:
* - The root of the binary tree is at level index 0, its children are at level index 1, their
* children are at level index 2, etc.
* - For every even-indexed level, all nodes at the level have odd integer values in strictly
* increasing order (from left to right).
* - For every odd-indexed level, all nodes at the level have even integer values in strictly
* decreasing order (from left to right).
*
* Given the root of a binary tree, return true if the binary tree is Even-Odd, otherwise
* return false.
*/
/**
* 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)
* }
*/
/**
* @param {TreeNode} root
* @return {boolean}
*/
var isEvenOddTree = function(root) {
let queue = [root];
let level = 0;
while (queue.length) {
const levelSize = queue.length;
let prevValue = level % 2 === 0 ? -Infinity : Infinity;
const newQueue = [];
for (let i = 0; i < levelSize; i++) {
const node = queue[i];
const isEvenLevel = level % 2 === 0;
const isOddValue = node.val % 2 === 1;
if (isEvenLevel && (!isOddValue || node.val <= prevValue)) return false;
if (!isEvenLevel && (isOddValue || node.val >= prevValue)) return false;
prevValue = node.val;
if (node.left) newQueue.push(node.left);
if (node.right) newQueue.push(node.right);
}
queue = newQueue;
level++;
}
return true;
};