Description
Submission
/**
* 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) {
queue<TreeNode*> q;
q.push(root);
for(int isEven = 1; !q.empty(); isEven = (isEven + 1) % 2) {
int count = q.size();
int prev = isEven ? numeric_limits<int>::min() : numeric_limits<int>::max();
for(int i = 0; i < count; ++i) {
TreeNode* curNode = q.front();
q.pop();
if(isEven) {
if(curNode->val % 2 == 0) return false;
if(curNode->val <= prev) return false;
} else {
if(curNode->val % 2 == 1) return false;
if(curNode->val >= prev) return false;
}
if(curNode->left) q.push(curNode->left);
if(curNode->right) q.push(curNode->right);
prev = curNode->val;
}
}
return true;
}
};