当前位置: 代码迷 >> 综合 >> Leetcode 1609. 奇偶树(DAY 16)
  详细解决方案

Leetcode 1609. 奇偶树(DAY 16)

热度:104   发布时间:2023-11-17 20:28:46.0

原题题目

在这里插入图片描述



代码实现(首刷自解)

/*** Definition for a binary tree node.* struct TreeNode {* int val;* struct TreeNode *left;* struct TreeNode *right;* };*/#define MAX 100001
#define INF 1000001bool isEvenOddTree(struct TreeNode* root){
    if(!root)return false;struct TreeNode* queue[MAX],*pos = NULL;int top = -1,rear = -1,size = 0,num,prenum,level = 0;queue[++rear] = root;while(rear != top){
    size = rear - top;if(!(level % 2))prenum = 0;elseprenum = INF;while(size--){
    pos = queue[++top];num = pos->val;if(!(level % 2)){
    if(num <= prenum || !(num % 2))return false;elseprenum = num;}else{
    if(num >= prenum || num % 2 )return false;elseprenum = num;}if(pos->left) queue[++rear] = pos->left;if(pos->right) queue[++rear] = pos->right;}level++;}return true;
}