Skip to content
QuantReadySign In
#060easyTrees

Identical Binary Trees

Time Limit: 2sMemory: 256MB

Problem

Given the roots of two binary trees, determine whether they are identical — that is, they have the same structure and every corresponding pair of nodes has the same value.

Input Format

Two lines, each containing space-separated values representing a tree in level-order. Use null for missing nodes. An empty line represents an empty tree.

Output Format

Print true if the two trees are identical, false otherwise.

Examples

Example 1
Input
100 50 150 25 75
100 50 150 25 75
Output
true

Both trees have identical structure and node values.

Example 2
Input
100 50 150
100 50 200
Output
false

The right child differs: 150 vs 200.

Constraints

  • 0 ≤ number of nodes in each tree ≤ 100
  • -10^4 ≤ node value ≤ 10^4
Loading interactive editor…