#C3924. Identical Binary Trees
Identical Binary Trees
Identical Binary Trees
You are given two binary trees, each represented using a preorder traversal string. The nodes are separated by spaces and the string null
represents an empty (or null) node. Two binary trees are considered identical if they have the same structure and identical node values.
Your task is to determine whether the two given binary trees are identical.
Note: You must read the input from stdin
and write the output to stdout
. The output should be either True
or False
(without quotes).
Preorder Traversal Format: For a binary tree, a preorder traversal is defined as visiting the current node first, then recursively traversing the left subtree, followed by the right subtree. For example, the binary tree:
1 / \ 2 3 / \ 4 5
is represented as: 1 2 null null 3 4 null null 5 null null
.
inputFormat
The input consists of two lines. Each line contains a preorder traversal string representing a binary tree. The tokens are separated by spaces and the token null
represents an empty node.
For example:
1 2 null null 3 4 null null 5 null null 1 2 null null 3 4 null null 5 null null
outputFormat
Output a single line containing True
if the two trees are identical, or False
otherwise.
1 2 null null 3 4 null null 5 null null
1 2 null null 3 4 null null 5 null null
True