All topics
66

Comparing trees, and checking a shape

Tier 1Locked

Where it runs

1. The problem

You have two versions of the same thing. For example, the config you saved and the config the server is running with right now. Or the last render of the interface and the new one. The question: are they exactly the same, with the same shape and the same values everywhere? If they are, the framework can skip redrawing the screen, and a backup tool doesn't upload the file again.

You can ask the same tree another question: is it symmetric, does its left side match the mirror image of its right side? And a third: is there a path from the root to a leaf whose values add up to a given number (a budget, a score threshold)?

In the last lessons the recursion walked one tree. Here it walks two places at once, in step: two trees, or the two halves of one tree.

This step is part of the paid track

The first 33 steps are open to everyone: lesson, exercise and tests. This one opens with full access. Start with a free account and the free steps.

Create a free accountSee what is included

On LeetCode

Problems that compare two trees or check a property along root to leaf paths. Recurse on matching pairs of nodes, or carry a running value down the tree.

We use necessary cookies to run the site (sign-in and language). If you accept, we also load Google Analytics to see which pages are used, and Google reCAPTCHA to keep spam off the contact and bug-report forms. Privacy policy