Skip to content

Commit 496957d

Browse files
committed
Add solution #1609
1 parent 89b9e4e commit 496957d

File tree

2 files changed

+59
-1
lines changed

2 files changed

+59
-1
lines changed

README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# 1,417 LeetCode solutions in JavaScript
1+
# 1,418 LeetCode solutions in JavaScript
22

33
[https://leetcodejavascript.com](https://leetcodejavascript.com)
44

@@ -1241,6 +1241,7 @@
12411241
1604|[Alert Using Same Key-Card Three or More Times in a One Hour Period](./solutions/1604-alert-using-same-key-card-three-or-more-times-in-a-one-hour-period.js)|Medium|
12421242
1605|[Find Valid Matrix Given Row and Column Sums](./solutions/1605-find-valid-matrix-given-row-and-column-sums.js)|Medium|
12431243
1608|[Special Array With X Elements Greater Than or Equal X](./solutions/1608-special-array-with-x-elements-greater-than-or-equal-x.js)|Easy|
1244+
1609|[Even Odd Tree](./solutions/1609-even-odd-tree.js)|Medium|
12441245
1657|[Determine if Two Strings Are Close](./solutions/1657-determine-if-two-strings-are-close.js)|Medium|
12451246
1668|[Maximum Repeating Substring](./solutions/1668-maximum-repeating-substring.js)|Easy|
12461247
1669|[Merge In Between Linked Lists](./solutions/1669-merge-in-between-linked-lists.js)|Medium|

solutions/1609-even-odd-tree.js

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/**
2+
* 1609. Even Odd Tree
3+
* https://leetcode.com/problems/even-odd-tree/
4+
* Difficulty: Medium
5+
*
6+
* A binary tree is named Even-Odd if it meets the following conditions:
7+
* - The root of the binary tree is at level index 0, its children are at level index 1, their
8+
* children are at level index 2, etc.
9+
* - For every even-indexed level, all nodes at the level have odd integer values in strictly
10+
* increasing order (from left to right).
11+
* - For every odd-indexed level, all nodes at the level have even integer values in strictly
12+
* decreasing order (from left to right).
13+
*
14+
* Given the root of a binary tree, return true if the binary tree is Even-Odd, otherwise
15+
* return false.
16+
*/
17+
18+
/**
19+
* Definition for a binary tree node.
20+
* function TreeNode(val, left, right) {
21+
* this.val = (val===undefined ? 0 : val)
22+
* this.left = (left===undefined ? null : left)
23+
* this.right = (right===undefined ? null : right)
24+
* }
25+
*/
26+
/**
27+
* @param {TreeNode} root
28+
* @return {boolean}
29+
*/
30+
var isEvenOddTree = function(root) {
31+
let queue = [root];
32+
let level = 0;
33+
34+
while (queue.length) {
35+
const levelSize = queue.length;
36+
let prevValue = level % 2 === 0 ? -Infinity : Infinity;
37+
const newQueue = [];
38+
39+
for (let i = 0; i < levelSize; i++) {
40+
const node = queue[i];
41+
const isEvenLevel = level % 2 === 0;
42+
const isOddValue = node.val % 2 === 1;
43+
44+
if (isEvenLevel && (!isOddValue || node.val <= prevValue)) return false;
45+
if (!isEvenLevel && (isOddValue || node.val >= prevValue)) return false;
46+
47+
prevValue = node.val;
48+
if (node.left) newQueue.push(node.left);
49+
if (node.right) newQueue.push(node.right);
50+
}
51+
52+
queue = newQueue;
53+
level++;
54+
}
55+
56+
return true;
57+
};

0 commit comments

Comments
 (0)