FunctionRacerProblemsRace

Leaf-Similar Trees

EasyTreesPython, JavaScript, TypeScript, Java, C++

The leaf sequence of a tree is the list of its leaf values read from left to right. Return True if the two trees root1 and root2 have exactly the same leaf sequence, otherwise False. The shapes of the trees may differ.

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

root1 [3,5,1,6,2,9,8,null,null,7,4]
root2 [3,5,1,6,7,4,2,null,null,null,null,null,null,9,8]
returns true

Both leaf sequences are 6, 7, 4, 9, 8.

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