1609.cpp (884B)
1 class Solution { 2 public: 3 bool isEvenOddTree(TreeNode *root) { 4 if (!root) return false; 5 6 queue<TreeNode *> q; 7 TreeNode *de = new TreeNode(0); 8 TreeNode *dd = new TreeNode(INT_MAX); 9 10 q.push(root); 11 for (int lvl = 0; !q.empty(); lvl++) { 12 TreeNode *p = (lvl % 2 == 0) ? de : dd; 13 for (int t = q.size(); t > 0; t--) { 14 TreeNode *root = q.front(); 15 q.pop(); 16 if (lvl % 2 == 0) { 17 if (root->val % 2 != 1 || root->val <= p->val) return false; 18 } else { 19 if (root->val % 2 != 0 || root->val >= p->val) return false; 20 } 21 p = root; 22 if (root->left) q.push(root->left); 23 if (root->right) q.push(root->right); 24 } 25 } 26 return true; 27 } 28 };