/**
 * public class TreeNode {
 *   public int key;
 *   public TreeNode left;
 *   public TreeNode right;
 *   public TreeNode(int key) {
 *     this.key = key;
 *   }
 * }
 */
public class Solution {
  public boolean isTweakedIdentical(TreeNode one, TreeNode two) {
    // Write your solution here.
    if (one == null && two == null) {
      return true;
    }
    if (one == null || two == null) {
      return false;
    }
    if (one.key != two.key) {
      return false;
    }
    return isTweakedIdentical(one.left, two.left) && isTweakedIdentical(one.right, two.right) || 
           isTweakedIdentical(one.left, two.right) && isTweakedIdentical(one.right, two.left);
  }
}

可翻转多次, 所以有两种情况。 (left.left, right.right) && (left,right, right.left) || (left.left, right.left) && (left.right, right.right)

O(n^2) 每次分四个叉, with log(2n) 层。 O(4^log(2n)) ==> O(n^2)

results matching ""

    No results matching ""