Problem 34 of 75

Leaf-Similar Trees

EasyNot solved yetInterview me on this
Before you code · which pattern fits?

The problem

The leaf value sequence of a binary tree is the values of its leaves read from left to right. Two trees are leaf-similar if their leaf value sequences are the same. Given the roots of two trees, return true if they are leaf-similar.

[3,5,1,6,2,9,8,null,null,7,4] vs [3,5,1,6,7,4,2,null,null,null,null,null,null,9,8] → true

Your solution runs against 2 unit tests, including edge and large-value cases.

Loading...

Test console · 2 unit tests

Submit your code to run it against all 2 unit tests. Results appear here.

JavaScript, TypeScript & Python run sandboxed in your browser; other languages run on the execution server. Your code is saved locally as you type.

Hint

Reference solution

Video explanation

Leaf-Similar Trees, explainedNeetCodeIO