
Even Odd Tree (medium)
Problem Statement
Given a binary tree, return true if it is an Even-Odd tree. Otherwise, return false.
The Even-odd tree must follow below two rules:
- At every
even-indexedlevel (starting from 0), all node values must beoddand arranged instrictly increasingorder fromlefttoright. - At every
odd-indexedlevel, all node values must beevenand arranged instrictly decreasingorder fromlefttoright.
Examples
Example 1
- Input:
1
/ \
10 4
/ \
3 7
- Expected Output:
true - Justification: The tree follows both conditions for each odd and even level. So, it is an
odd-eventree.
Example 2
Input:
5
/ \
9 3
/ \
12 8
- Expected Output:
false - Justification: Level 1 has Odd values 9 and 3 in decreasing order, but it should have even values. So, the tree is not an
odd-eventree.
Example 3
- Input:
7
/ \
10 2
/ \
12 8
- Expected Output:
false - Justification: At level 2 (even-indexed), the values are 12 and 8, which are even, but they should have odd values. So, the tree is not an
odd-eventree.
Constraints:
- The number of nodes in the tree is in the range [1, 10<sup>5</sup>].
- 1 <= Node.val <= 10<sup>6</sup>
Try it yourself
Try solving this question here:
Python3
Python3
. . . .
.....
.....
.....
Unlock this and all other premium problems.
No code editor for this lesson
This lesson focuses on concepts and theory