<p>A binary tree is named <strong>Even-Odd</strong> if it meets the following conditions:</p>

<ul>
	<li>The root of the binary tree is at level index <code>0</code>, its children are at level index <code>1</code>, their children are at level index <code>2</code>, etc.</li>
	<li>For every <strong>even-indexed</strong> level, all nodes at the level have <strong>odd</strong> integer values in <strong>strictly increasing</strong> order (from left to right).</li>
	<li>For every <b>odd-indexed</b> level, all nodes at the level have <b>even</b> integer values in <strong>strictly decreasing</strong> order (from left to right).</li>
</ul>

<p>Given the <code>root</code> of a binary tree, <em>return </em><code>true</code><em> if the binary tree is <strong>Even-Odd</strong>, otherwise return </em><code>false</code><em>.</em></p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/09/15/sample_1_1966.png" style="width: 362px; height: 229px;" />
<pre>
<strong>Input:</strong> root = [1,10,4,3,null,7,9,12,8,6,null,null,2]
<strong>Output:</strong> true
<strong>Explanation:</strong> The node values on each level are:
Level 0: [1]
Level 1: [10,4]
Level 2: [3,7,9]
Level 3: [12,8,6,2]
Since levels 0 and 2 are all odd and increasing and levels 1 and 3 are all even and decreasing, the tree is Even-Odd.
</pre>

<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/09/15/sample_2_1966.png" style="width: 363px; height: 167px;" />
<pre>
<strong>Input:</strong> root = [5,4,2,3,3,7]
<strong>Output:</strong> false
<strong>Explanation:</strong> The node values on each level are:
Level 0: [5]
Level 1: [4,2]
Level 2: [3,3,7]
Node values in level 2 must be in strictly increasing order, so the tree is not Even-Odd.
</pre>

<p><strong class="example">Example 3:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/09/22/sample_1_333_1966.png" style="width: 363px; height: 167px;" />
<pre>
<strong>Input:</strong> root = [5,9,1,3,5,7]
<strong>Output:</strong> false
<strong>Explanation:</strong> Node values in the level 1 should be even integers.
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li>The number of nodes in the tree is in the range <code>[1, 10<sup>5</sup>]</code>.</li>
	<li><code>1 &lt;= Node.val &lt;= 10<sup>6</sup></code></li>
</ul>

nprint = lambda *l: print(", ".join(str(i.val if i else "None") for i in l))

class Solution:
    def isEvenOddTree(self, root: Optional[TreeNode]) -> bool:
        isOdd = lambda i: i%2==1
        isEven = lambda i: i%2==0
        def recursion(node, level):
            if node:
                if not (isOdd if isEven(level) else isEven)(node.val):
                    return False
                if level not in hm:
                    hm[level] = node.val
                else:
                    if isEven(level) and hm[level]>= node.val:
                        return False
                    elif isOdd(level) and hm[level]<=node.val:
                        return False
                    hm[level] = node.val
                left = recursion(node.left, level+1)
                right = recursion(node.right, level+1)
                return left and right
            return True
        hm = {}
        ans = recursion(root, 0)
        return ans
        
    def isEvenOddTree(self, root: Optional[TreeNode]) -> bool:
        isOdd = lambda i: i%2==1
        isEven = lambda i: i%2==0
        stack = [(root, 0)]
        hm = {}
        while stack:
            node, level = stack.pop()
            if node:
                if not (isOdd if isEven(level) else isEven)(node.val):
                    return False
                if level not in hm:
                    hm[level] = node.val
                else:
                    if isEven(level) and hm[level]>= node.val:
                        return False
                    elif isOdd(level) and hm[level]<=node.val:
                        return False
                    hm[level] = node.val
                stack.append((node.right, level+1))
                stack.append((node.left, level+1))
        return True
    def isEvenOddTree(self, root: Optional[TreeNode]) -> bool:
        isOdd = lambda i: i%2==1
        isEven = lambda i: i%2==0
        queue = [root]
        next_queue = []
        level = 0
        prev = None
        while queue:
            nprint(*queue)
            while queue:
                node = queue.pop(0)
                if node:
                    if not (isOdd if isEven(level) else isEven)(node.val):
                        return False
                    if not prev:
                        prev = node.val
                    else:
                        if isEven(level) and prev>= node.val:
                            return False
                        elif isOdd(level) and prev<=node.val:
                            return False
                        prev = node.val
                next_queue.append(node.left) if node.left else None
                next_queue.append(node.right) if node.right else None
            queue = next_queue
            next_queue = []
            prev = None
            level+=1
        return True

38285