Skip to content

Commit 981a7df

Browse files
authored
Create 1609-even-odd-tree.js
1 parent 2fa4efd commit 981a7df

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

1609-even-odd-tree.js

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* function TreeNode(val, left, right) {
4+
* this.val = (val===undefined ? 0 : val)
5+
* this.left = (left===undefined ? null : left)
6+
* this.right = (right===undefined ? null : right)
7+
* }
8+
*/
9+
/**
10+
* @param {TreeNode} root
11+
* @return {boolean}
12+
*/
13+
const isEvenOddTree = function(root) {
14+
const q = [root]
15+
const v = []
16+
let l = 0
17+
while(q.length) {
18+
const size = q.length
19+
const row = []
20+
for(let i = 0; i < size; i++) {
21+
const cur = q.shift()
22+
row.push(cur.val)
23+
if(l % 2 === 0 && cur.val % 2 === 0) return false
24+
if(l % 2 === 1 && cur.val % 2 === 1) return false
25+
if(row.length > 1) {
26+
if(l % 2 === 0 && row[row.length - 1] <= row[row.length - 2]) return false
27+
if(l % 2 === 1 && row[row.length - 1] >= row[row.length - 2]) return false
28+
}
29+
if(cur.left) q.push(cur.left)
30+
if(cur.right) q.push(cur.right)
31+
}
32+
l++
33+
}
34+
return true
35+
};

0 commit comments

Comments
 (0)