August 26, 2024

P56 - Symmetric binary trees.

Let us call a binary tree symmetric if you can draw a vertical line through the root node and then the right subtree is the mirror image of the left subtree. Add anisSymmetricmethod to the Tree class to check whether a given binary tree is symmetric.

Hint: Write anisMirrorOfmethod first to check whether one tree is the mirror image of another. We are only interested in the structure, not in the contents of the nodes.

> Node('a', Node('b'), Node('c')).isSymmetric
  res0: Boolean = true
Be first to comment
Leave a reply