File tree 1 file changed +35
-0
lines changed
1 file changed +35
-0
lines changed Original file line number Diff line number Diff line change
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
+ } ;
You can’t perform that action at this time.
0 commit comments