Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When two trees are equal?

If in-order traversal of two binrary trees (not binary search trees) are the same, does it guarantee that the two trees are the same?

if answer is no, then what about both in-order and pre-order traversal are the same?

like image 638
Lily Avatar asked Oct 22 '10 21:10

Lily


People also ask

How do you compare two trees equal?

Solution Breakdown Two trees 'A' and 'B' are identical if: data on their roots is the same or both roots are null. left sub-tree of 'A' is identical to the left sub-tree of 'B' right sub-tree of 'A' is identical to the right sub-tree of 'B'

Is same binary tree?

Two binary trees are identical if: their root nodes have the same value, their left subtree is identical, their right subtree is identical.


1 Answers

Definitely not. The two trees

  b
 / \
a   d
   / \
  c   e

and

    d
   / \
  b   e
 / \
a   c

both have an inorder traversal of a b c d e. They are, in fact, rotations, an operation which preserves inorder traversal.

like image 89
Josh Lee Avatar answered Sep 29 '22 19:09

Josh Lee