22 · Tree Recursion

#100 Same Tree

Easysolve on LeetCode ↗

frequently reported atBloombergAmazonGoogleMicrosoftMetaLinkedIn

the problem

In plain words

Return true if two binary trees have the same shape and the same values in the same places.

in p = [1,2,3], q = [1,2,3]

out true

in p = [1,2], q = [1,null,2]

out false

Same values, different shape.

in p = [1,2,1], q = [1,1,2]

out false

  • 0 ≤ number of nodes in each tree ≤ 100
  • -10⁴ ≤ Node.val ≤ 10⁴

your turn

Try it before you peek

Read the examples, say the brute force out loud, then write code: here or on LeetCode ↗. Stuck for 20 minutes? Open one hint at a time.

Solve it in the browser

LeetCode-style: complete the class. Compiled by the real javac (Java 17) and judged right here — nothing is sent anywhere.

    Solutions are hidden so you can try first. Even 20 minutes of real effort makes the answer stick far better than reading it.

    Marking the problem solved, or opening every hint, also shows it.

    practise next

    Similar problems

    1. #101
      Symmetric Tree
      Easyin Tree Recursion
    2. #572
      Subtree of Another Tree
      Easyin Tree Recursion
    3. #226
      Invert Binary Tree
      Easyin Tree Recursion
    4. #297