I\'m looking at an interview book and the question is:
You have two very large binary trees:
T1
, with millions of nodes, andT2
Here is a counter-example to the method.
Consider the tree T1
:
B
/ \
A D
/ \
C E
\
F
And the sub-tree T2
:
D
/ \
C E
The relevant traversals are:
T1
pre-order: BADCEF
T2
pre-order: DCE
T1
in-order: ABCDEF
T2
in-order: CDE
While DCE
is in BADCEF
and CDE
is in ABCDEF
, T2
is not actually a sub-tree of T1
. The author's definition of sub-tree must have been different or it was just a mistake.
Related question: Determine if a binary tree is subtree of another binary tree using pre-order and in-order strings