Check if two given binary trees are identical. Identical means the equal valued keys are at the same position in the two binary trees.

Solution: 注意base case

  public boolean isIdentical(TreeNode one, TreeNode two) {
    if (one == null && two == null) {
      return true;
    }
    if (one == null || two == null || one.key != two.key) {
      return false;
    }
    return isIdentical(one.left, two.left) && isIdentical(one.right, two.right);
  }

results matching ""

    No results matching ""