|
| 1 | +<h2><a href="https://leetcode.com/problems/even-odd-tree/">1609. Even Odd Tree</a></h2><h3>Medium</h3><hr><div><p>A binary tree is named <strong>Even-Odd</strong> if it meets the following conditions:</p> |
| 2 | + |
| 3 | +<ul> |
| 4 | + <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> |
| 5 | + <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> |
| 6 | + <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> |
| 7 | +</ul> |
| 8 | + |
| 9 | +<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> |
| 10 | + |
| 11 | +<p> </p> |
| 12 | +<p><strong class="example">Example 1:</strong></p> |
| 13 | +<img alt="" src="https://assets.leetcode.com/uploads/2020/09/15/sample_1_1966.png" style="width: 362px; height: 229px;"> |
| 14 | +<pre><strong>Input:</strong> root = [1,10,4,3,null,7,9,12,8,6,null,null,2] |
| 15 | +<strong>Output:</strong> true |
| 16 | +<strong>Explanation:</strong> The node values on each level are: |
| 17 | +Level 0: [1] |
| 18 | +Level 1: [10,4] |
| 19 | +Level 2: [3,7,9] |
| 20 | +Level 3: [12,8,6,2] |
| 21 | +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. |
| 22 | +</pre> |
| 23 | + |
| 24 | +<p><strong class="example">Example 2:</strong></p> |
| 25 | +<img alt="" src="https://assets.leetcode.com/uploads/2020/09/15/sample_2_1966.png" style="width: 363px; height: 167px;"> |
| 26 | +<pre><strong>Input:</strong> root = [5,4,2,3,3,7] |
| 27 | +<strong>Output:</strong> false |
| 28 | +<strong>Explanation:</strong> The node values on each level are: |
| 29 | +Level 0: [5] |
| 30 | +Level 1: [4,2] |
| 31 | +Level 2: [3,3,7] |
| 32 | +Node values in level 2 must be in strictly increasing order, so the tree is not Even-Odd. |
| 33 | +</pre> |
| 34 | + |
| 35 | +<p><strong class="example">Example 3:</strong></p> |
| 36 | +<img alt="" src="https://assets.leetcode.com/uploads/2020/09/22/sample_1_333_1966.png" style="width: 363px; height: 167px;"> |
| 37 | +<pre><strong>Input:</strong> root = [5,9,1,3,5,7] |
| 38 | +<strong>Output:</strong> false |
| 39 | +<strong>Explanation:</strong> Node values in the level 1 should be even integers. |
| 40 | +</pre> |
| 41 | + |
| 42 | +<p> </p> |
| 43 | +<p><strong>Constraints:</strong></p> |
| 44 | + |
| 45 | +<ul> |
| 46 | + <li>The number of nodes in the tree is in the range <code>[1, 10<sup>5</sup>]</code>.</li> |
| 47 | + <li><code>1 <= Node.val <= 10<sup>6</sup></code></li> |
| 48 | +</ul> |
| 49 | +</div> |
0 commit comments