← Home

1609. Even Odd Tree

LeetCode article · C++ solution
Website made by wuisabel-gif · Original C++ code by keineahnung2345
two pointersC++Markdown
160

I like to read this solution as a small machine: keep the useful information, throw away the noise. For 1609. Even Odd Tree, the solution in this repository is mainly a two pointers solution.

Guide

What?

The code is easier to read if we treat it as a controlled search through possible states. Instead of trying to be clever immediately, read the code as a sequence of questions:

  • What state are we keeping?
  • How do we move from one state to the next?
  • When do we know the answer is already determined?

For this file, the main tools are: two pointers, sliding window.

The notes already sitting in the source point us in the right direction:

  • BFS

Guide

When?

Reach for this shape when a direct simulation would work logically but waste time revisiting the same information. The accepted code reduces that pressure by storing exactly the information that remains useful later.

The important function names to track are isEvenOddTree.

Guide

Why?

The win comes from making each line carry responsibility: store the useful state, discard the rest, keep moving.

  • The queue gives the solution a level-by-level or frontier-style traversal.
  • The final return is not magic; it is the invariant after the loops or recursion have finished doing their accounting.

Guide

How?

Walk through the solution in this order:

  1. Initialize the memory or helper structure.
  2. Process candidates in the order the invariant expects.
  3. Update the answer only when the current state is valid.
  4. Return the value that represents the fully processed input.

The most important competitive-programming habit here is to trust the invariant. Once the invariant is right, the loops become much less scary.

Guide

Complexity

  • Time: O(n) to O(n log n), depending on the dominant loop or data structure operation
  • Space: O(n) in the usual case for auxiliary containers or recursion

Guide

C++ Solution

Your submission

The accepted solution

solution.cpp
01//BFS
02//Runtime: 344 ms, faster than 75.00% of C++ online submissions for Even Odd Tree.
03//Memory Usage: 152.7 MB, less than 25.00% of C++ online submissions for Even Odd Tree.
04/**
05 * Definition for a binary tree node.
06 * struct TreeNode {
07 *     int val;
08 *     TreeNode *left;
09 *     TreeNode *right;
10 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
11 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
12 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
13 * };
14 */
15class Solution {
16public:
17    bool isEvenOddTree(TreeNode* root) {
18        queue<TreeNode*> q;
19        TreeNode* cur;
20        int levelId = 0;
21        int last;
22        
23        q.push(root);
24        
25        while(!q.empty()){
26            int levelSize = q.size();
27            
28            if(!(levelId&1)){
29                //even level, increasing
30                last = INT_MIN;
31            }else{
32                last = INT_MAX;
33            }
34            
35            while(levelSize-- > 0){
36                cur = q.front(); q.pop();
37                
38                if(!(levelId&1)){
39                    if(!(cur->val&1) || cur->val <= last){
40                        return false;
41                    }
42                }else{
43                    if((cur->val&1) || cur->val >= last){
44                        return false;
45                    }
46                }
47                last = cur->val;
48                
49                if(cur->left) q.push(cur->left);
50                if(cur->right) q.push(cur->right);
51            }
52            
53            ++levelId;
54        }
55        
56        return true;
57    }
58};

Cost

Complexity

Time
O(n) to O(n log n), depending on the dominant loop or data structure operation
Dominated by the main traversal, recursion, or data-structure operations in the code.
Space
O(n) in the usual case for auxiliary containers or recursion
Auxiliary state plus the answer structure where the problem requires one.