当前位置:网站首页>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;
}
}边栏推荐
- 2022.6.6-----leetcode.732
- SAS principal component analysis (finding correlation matrix, eigenvalue, unit eigenvector, principal component expression, contribution rate and cumulative contribution rate, and data interpretation)
- SAS判别分析(Bayes准则和proc discrim过程)
- 负数+0+正数
- Yunna Qingyuan fixed assets management and barcode inventory system
- 焱融看|混合云环境下,如何实现数据湖最优存储解决方案
- What is the C-end and what is the b-end? Let me tell you
- Understanding of multithreading
- 1.6 Px4 initialization calibration
- A tutorial on building a website from scratch with complete steps (7000 words and 102 screenshots for everyone to understand, with source code attached)
猜你喜欢

Garbled code when the command parameter contains% in VisualStudio debugging

2021-02-27MATLAB的图像处理

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

Understanding of multithreading

云呐|PDA无线固定资产盘点管理系统

Current limiting and download interface request number control

1.4PX4程序下载

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

SAS判别分析(Bayes准则和proc discrim过程)

QGC地面站使用教程
随机推荐
I was so excited about the college entrance examination in 2022
Linux安装mysql数据库详解
2022.6.6-----leetcode.732
SAS期末复习知识点总结(应用多元统计实验笔记)
[ongoing update...] 2021 National Electronic Design Competition for college students (III) interpretation of the anonymous four axis space developer flight control system design
Daily problem essay | 21.11.29: use resttemplate to call external put request, and prompt '400 bad request'
What is the C-end and what is the b-end? Let me tell you
云呐|PDA无线固定资产盘点管理系统
立个flag--重构promise
Bad RequestThis combination of host and port requires TLS.
Introduction to the application process of Beijing China Patent Award, with a subsidy of 1million yuan
Px4 installation tutorial (VI) vertical fixed wing (tilting)
Introduction to the policy support of Shenzhen China Patent Award, with a subsidy of 1million yuan
SAS discriminant analysis (Bayes criterion and proc discrim process)
Leetcode permutation and combination problem backtracking
复利的保险理财产品怎么样?可以买吗?
1.6、 PX4初始化校准
Project_ Visual analysis of epidemic data based on Web Crawler
Is the SQL query result different from what you expected? Mostly "null" is making trouble
Detailed explanation of classic papers on OCR character recognition