当前位置:网站首页>LeetCode 1609 Even Odd Tree (bfs)
LeetCode 1609 Even Odd Tree (bfs)
2022-06-11 01:44:00 【_ TCgogogo_】
A binary tree is named Even-Odd if it meets the following conditions:
- The root of the binary tree is at level index
0, its children are at level index1, their children are at level index2, etc. - For every even-indexed level, all nodes at the level have odd integer values in strictly increasing order (from left to right).
- For every odd-indexed level, all nodes at the level have even integer values in strictly decreasing order (from left to right).
Given the root of a binary tree, return true if the binary tree is Even-Odd, otherwise return false.
Example 1:

Input: root = [1,10,4,3,null,7,9,12,8,6,null,null,2] Output: true Explanation: 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.
Example 2:

Input: root = [5,4,2,3,3,7] Output: false Explanation: 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.
Example 3:

Input: root = [5,9,1,3,5,7] Output: false Explanation: Node values in the level 1 should be even integers.
Constraints:
- The number of nodes in the tree is in the range
[1, 10^5]. 1 <= Node.val <= 10^6
Topic link :https://leetcode.com/problems/even-odd-tree/
The main idea of the topic : Ask whether the tree is full of odd layers, even and strictly decreasing , Even layers are all odd and strictly increasing
Topic analysis : Level traversal , Judge according to the conditions
15ms, Time beats 71.4%
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
public boolean isEvenOddTree(TreeNode root) {
Queue<TreeNode> q = new LinkedList<>();
q.offer(root);
int h = 0;
while (!q.isEmpty()) {
int sz = q.size(), ma = Integer.MIN_VALUE, mi = Integer.MAX_VALUE;
for (int i = 0; i < sz; i++) {
TreeNode cur = q.poll();
if (h % 2 == cur.val % 2) {
return false;
}
if (h % 2 == 0) {
if (cur.val <= ma) {
return false;
}
ma = cur.val;
} else {
if (cur.val >= mi) {
return false;
}
mi = cur.val;
}
if (cur.left != null) {
q.offer(cur.left);
}
if (cur.right != null) {
q.offer(cur.right);
}
}
h++;
}
return true;
}
}边栏推荐
猜你喜欢

What is the C-end and what is the b-end? Let me tell you

OCR文字识别经典论文详解
![[Li mu] how to read papers [intensive reading of papers]](/img/41/7e1ff1db2f7a848c8702c186c79fe5.jpg)
[Li mu] how to read papers [intensive reading of papers]

Detailed explanation of classic papers on OCR character recognition

多兴趣召回模型实践|得物技术
![[path planning] week 1: hodgepodge](/img/80/074b847c6826b306318aeb9866a829.jpg)
[path planning] week 1: hodgepodge

Leetcode search questions

Px4 from abandonment to mastery (twenty four): customized model

PX4从放弃到精通(二十四):自定义机型

Inventory management and strategy mode
随机推荐
Linux安装mysql数据库详解
I was so excited about the college entrance examination in 2022
Lazy singleton mode
2.0 detailed explanation of ROS and Px4 communication
How about compound interest insurance and financial products? Can I buy it?
2.1 ros+px4 simulation - Fixed Point flight control
Leetcode 1814 count nice pairs in an array (recommended by map)
Middleware_ Redis_ 05_ Persistence of redis
CSRF attack
Multi interest recall model practice | acquisition technology
threejs:流光效果封装
Summary of SAS final review knowledge points (notes on Application of multivariate statistics experiment)
2021-3-1MATLAB写cnn的mnist数据库训练
ROS parameter server
Sealem finance builds Web3 decentralized financial platform infrastructure
SAS cluster analysis (system cluster, dynamic cluster fastclus, variable cluster varclus)
云呐|PDA无线固定资产盘点管理系统
懒汉式单例模式
Yunna Qingyuan fixed assets management and barcode inventory system
Docking of express bird system