FunctionRacerProblemsRace

Even-Odd Tree

MediumTreesPython, JavaScript, TypeScript, Java, C++

You are given the root of a binary tree. Check these rules, where the root is on level 0:

Return True if every level follows its rule, otherwise False. A tree with a single node passes if its value is odd.

Trees appear in the tests in level order, with null for a missing child, like [3,9,20,null,null,15,7]. Each node is a TreeNode with .val, .left and .right; the class is already defined for you.

Example 1

root [1,10,4,3,null,7,9,12,8,6,null,null,2]
returns true

Every level follows its rule, so the answer is True.

Example 2

root [5,4,2,3,3,7]
returns false

Level 2 is [3, 3, 7]: not strictly increasing, so False.

Constraints

Race itor take your time

Write it in your browser, against the clock. The model solution, stepped through line by line, comes after.

More Trees problems