Tweaked Identical Binary Trees

Determine whether two given binary trees are identical assuming any number of ‘tweak’s are allowed. A tweak is defined as a swap of the children of one node in the tree.

Example:

        5

      /    \

    3        8

  /   \

1      4

and

        5

      /    \

    8        3

           /   \

          1     4

the two binary trees are tweaked identical.

How is the binary tree represented?

We use the level order traversal sequence with a special symbol "#" denoting the null node.

class Solution(object):
  def isTweakedIdentical(self, one, two):
    if one is None and two is None:
      return True
    if one and two and one.val == two.val:
      return self.isTweakedIdentical(one.left,two.right) and self.isTweakedIdentical(one.right,two.left) or self.isTweakedIdentical(one.left,two.left) and self.isTweakedIdentical(one.right,two.right)
    return False

你可能感兴趣的:(Tweaked Identical Binary Trees)