当前位置:网站首页>Leetcode -- parity tree
Leetcode -- parity tree
2022-04-22 22:02:00 【NewSuNess】
-------------------------------------------- Pure brush notes , Not your own way of solving problems -------------------------------------
、 Title Description :
If a binary tree satisfies the following conditions , It can be called Even tree :
The subscript of the layer where the root node of the binary tree is located is 0 , The layer of the child node of the root is subscript 1 , The layer of the root's grandson is subscript 2 , And so on .
Even subscript The values of all nodes on the layer are p. Integers , From left to right Strictly increasing
Odd subscript The values of all nodes on the layer are accidentally Integers , From left to right Strictly decreasing
Give you the root node of the binary tree , If the binary tree is Even tree , Then return to true , Otherwise return to false .
answer :
/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode() : val(0), left(nullptr), right(nullptr) {} * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} * }; */
class Solution {
public:
bool isEvenOddTree(TreeNode* root) {
// Breadth first traversal This topic breadth first search is indeed more appropriate It can match the current level
queue<TreeNode*> qu;
qu.push(root);
int level = 0;
while (!qu.empty()) {
int size = qu.size();
// cout<<size<<endl;
int prev = level % 2 == 0 ? INT_MIN : INT_MAX;
for (int i = 0; i < size; i++) {
// Returns the first element
TreeNode * node = qu.front();
// Pop up the first element
qu.pop();
cout<< i <<endl;
int value = node->val;
if (level % 2 == value % 2) {
return false;
}
if ((level % 2 == 0 && value <= prev) || (level % 2 == 1 && value >= prev)) {
return false;
}
prev = value;
if (node->left != nullptr) {
// Current level breadth traversal
qu.push(node->left);
}
if (node->right != nullptr) {
qu.push(node->right);
}
}
level++;
}
return true;
}
};
版权声明
本文为[NewSuNess]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204221531471047.html
边栏推荐
- 每日一练(47):找不同
- ES6 generator function and async function
- Demo example of SMS platform API interface - node / SMS / xsend
- “开源之夏”活动火热报名中,丰厚奖金等你来拿!
- Markdown advanced usage
- Leetcode question bank 62 Different paths (recursive C implementation)
- Go daily news (March 28, 2021) -- non type safe pointers in golang language
- 引航成长·匠心赋能——YonMaster开发者培训领航计划全面开启
- 为什么我们需要做企业成长性评价分析?
- Determination and analysis of Immundiagnostik idk turbopel
猜你喜欢
Browser HTTP cache related knowledge

Abbexa 4E-BP1 cell ELISA Kit instructions

8.1 sequence model

Quick off-line solution of LCA
Android uses SQLite kotlin
![[operation tutorial] how to turn on the voice intercom function on the national standard gb28181 platform easygbs?](/img/73/7f3e4f391c42cc1d69cce0afed18f5.png)
[operation tutorial] how to turn on the voice intercom function on the national standard gb28181 platform easygbs?

Penetration test & Network & CTF interview questions sorting

Prediction of loan risk with keras logistic regression

Remote desktop failure resolution

BACKBONE,NECK,HEAD
随机推荐
JMeter accesses the data of SQL Server with comma '' to CSV file
ES6 reflect object
Leaflet修改地图中心点定位
原来,这才是开发者打开世界读书日的正确姿势!
How to write word reading and color games based on JS
Kindeditor displays the source code mode by default on the mobile terminal
PlatoFarm几大创新经济模型,给予当下元宇宙市场的启发
Immunoagnostik CRP (C-reactive protein) ELISA analysis
playwright操作select下的下拉选项
“开源之夏”活动火热报名中,丰厚奖金等你来拿!
What should web testing pay attention to?
Suggestions for improving C program 4: implementation of standard dispose mode in C
MySQL configuration parameters and commands
超声波模块的使用(STM32/HAL)
The use of servlets corresponds to the nine built-in objects
8.1 sequence model
ThingsKit物联网平台
Dealers organize a group to review Auchan Z6: praise high-energy products
[weekly] April 17
Why do we need to do enterprise growth evaluation and analysis?