#C13344. Compare Inorder Traversals
Compare Inorder Traversals
Compare Inorder Traversals
Given two binary trees, determine whether their inorder traversal sequences are identical. Inorder traversal of a binary tree is defined as visiting the left child, then the node itself, and finally the right child. Formally, for any node n with left subtree L and right subtree R, the inorder sequence is given by:
$$\text{inorder}(n) = \text{inorder}(L) \Vert [n.val] \Vert \text{inorder}(R) $$Your task is to construct the binary trees from their level-order serialized input and compare the two inorder traversals. The serialization is given in a space-separated format where a token "null" represents an absent (empty) node.
If both inorder sequences match exactly, output True
; otherwise, output False
.
inputFormat
The input consists of two lines. The first line is the level-order serialization of the first binary tree, and the second line is the level-order serialization of the second binary tree. Each serialization is a space-separated list of tokens. Each token is either an integer (representing the node's value) or the string "null" (representing a missing node).
For example:
1 2 3 1 3 2
outputFormat
Output a single line containing either True
if the inorder traversals of both trees are identical or False
otherwise.
null
null
True