|
| 1 | +# [1609. Even Odd Tree](https://leetcode.com/problems/even-odd-tree/) |
| 2 | + |
| 3 | +## 题目 |
| 4 | + |
| 5 | +A binary tree is named Even-Odd if it meets the following conditions: |
| 6 | + |
| 7 | +- The root of the binary tree is at level index 0, its children are at level index 1, their children are at level index 2, etc. |
| 8 | +- For every even-indexed level, all nodes at the level have odd integer values in strictly increasing order (from left to right). |
| 9 | +- For every odd-indexed level, all nodes at the level have even integer values in strictly decreasing order (from left to right). |
| 10 | + |
| 11 | +Given the root of a binary tree, return true if the binary tree is Even-Odd, otherwise return false. |
| 12 | + |
| 13 | +**Example 1**: |
| 14 | + |
| 15 | + |
| 16 | + |
| 17 | + Input: root = [1,10,4,3,null,7,9,12,8,6,null,null,2] |
| 18 | + Output: true |
| 19 | + Explanation: The node values on each level are: |
| 20 | + Level 0: [1] |
| 21 | + Level 1: [10,4] |
| 22 | + Level 2: [3,7,9] |
| 23 | + Level 3: [12,8,6,2] |
| 24 | + 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. |
| 25 | + |
| 26 | +**Example 2**: |
| 27 | + |
| 28 | + |
| 29 | + |
| 30 | + Input: root = [5,4,2,3,3,7] |
| 31 | + Output: false |
| 32 | + Explanation: The node values on each level are: |
| 33 | + Level 0: [5] |
| 34 | + Level 1: [4,2] |
| 35 | + Level 2: [3,3,7] |
| 36 | + Node values in level 2 must be in strictly increasing order, so the tree is not Even-Odd. |
| 37 | + |
| 38 | +**Example 3**: |
| 39 | + |
| 40 | + |
| 41 | + |
| 42 | + Input: root = [5,9,1,3,5,7] |
| 43 | + Output: false |
| 44 | + Explanation: Node values in the level 1 should be even integers. |
| 45 | + |
| 46 | +**Example 4**: |
| 47 | + |
| 48 | + Input: root = [1] |
| 49 | + Output: true |
| 50 | + |
| 51 | +**Example 5**: |
| 52 | + |
| 53 | + Input: root = [11,8,6,1,3,9,11,30,20,18,16,12,10,4,2,17] |
| 54 | + Output: True |
| 55 | + |
| 56 | +**Constraints:** |
| 57 | + |
| 58 | +- The number of nodes in the tree is in the range [1, 100000]. |
| 59 | +- 1 <= Node.val <= 1000000 |
| 60 | + |
| 61 | +## 题目大意 |
| 62 | + |
| 63 | +如果一棵二叉树满足下述几个条件,则可以称为 奇偶树 : |
| 64 | + |
| 65 | +- 二叉树根节点所在层下标为 0 ,根的子节点所在层下标为 1 ,根的孙节点所在层下标为 2 ,依此类推。 |
| 66 | +- 偶数下标 层上的所有节点的值都是 奇 整数,从左到右按顺序 严格递增 |
| 67 | +- 奇数下标 层上的所有节点的值都是 偶 整数,从左到右按顺序 严格递减 |
| 68 | + |
| 69 | +给你二叉树的根节点,如果二叉树为 奇偶树 ,则返回 true ,否则返回 false 。 |
| 70 | + |
| 71 | +## 解题思路 |
| 72 | + |
| 73 | +- 广度优先遍历(分别判断奇数层和偶数层) |
| 74 | + |
| 75 | +## 代码 |
| 76 | + |
| 77 | +```go |
| 78 | +package leetcode |
| 79 | + |
| 80 | +type TreeNode struct { |
| 81 | + Val int |
| 82 | + Left *TreeNode |
| 83 | + Right *TreeNode |
| 84 | +} |
| 85 | + |
| 86 | +func isEvenOddTree(root *TreeNode) bool { |
| 87 | + level := 0 |
| 88 | + queue := []*TreeNode{root} |
| 89 | + for len(queue) != 0 { |
| 90 | + length := len(queue) |
| 91 | + var nums []int |
| 92 | + for i := 0; i < length; i++ { |
| 93 | + node := queue[i] |
| 94 | + if node.Left != nil { |
| 95 | + queue = append(queue, node.Left) |
| 96 | + } |
| 97 | + if node.Right != nil { |
| 98 | + queue = append(queue, node.Right) |
| 99 | + } |
| 100 | + nums = append(nums, node.Val) |
| 101 | + } |
| 102 | + if level%2 == 0 { |
| 103 | + if !even(nums) { |
| 104 | + return false |
| 105 | + } |
| 106 | + } else { |
| 107 | + if !odd(nums) { |
| 108 | + return false |
| 109 | + } |
| 110 | + } |
| 111 | + queue = queue[length:] |
| 112 | + level++ |
| 113 | + } |
| 114 | + return true |
| 115 | +} |
| 116 | + |
| 117 | +func odd(nums []int) bool { |
| 118 | + cur := nums[0] |
| 119 | + if cur%2 != 0 { |
| 120 | + return false |
| 121 | + } |
| 122 | + for _, num := range nums[1:] { |
| 123 | + if num >= cur { |
| 124 | + return false |
| 125 | + } |
| 126 | + if num%2 != 0 { |
| 127 | + return false |
| 128 | + } |
| 129 | + cur = num |
| 130 | + } |
| 131 | + return true |
| 132 | +} |
| 133 | + |
| 134 | +func even(nums []int) bool { |
| 135 | + cur := nums[0] |
| 136 | + if cur%2 == 0 { |
| 137 | + return false |
| 138 | + } |
| 139 | + for _, num := range nums[1:] { |
| 140 | + if num <= cur { |
| 141 | + return false |
| 142 | + } |
| 143 | + if num%2 == 0 { |
| 144 | + return false |
| 145 | + } |
| 146 | + cur = num |
| 147 | + } |
| 148 | + return true |
| 149 | +} |
| 150 | +``` |
| 151 | + |
| 152 | + |
| 153 | +---------------------------------------------- |
| 154 | +<div style="display: flex;justify-content: space-between;align-items: center;"> |
| 155 | +<p><a href="https://books.halfrost.com/leetcode/ChapterFour/1600~1699/1608.Special-Array-With-X-Elements-Greater-Than-or-Equal-X/">⬅️上一页</a></p> |
| 156 | +<p><a href="https://books.halfrost.com/leetcode/ChapterFour/1600~1699/1614.Maximum-Nesting-Depth-of-the-Parentheses/">下一页➡️</a></p> |
| 157 | +</div> |
0 commit comments