class Solution {
public Node intersect(Node quadTree1, Node quadTree2) {
if (quadTree1.isLeaf) {
if (quadTree1.val == true) {
return quadTree1;
} else {
return quadTree2;
}
}
if (quadTree2.isLeaf) {
if (quadTree2.val == true) {
return quadTree2;
} else {
return quadTree1;
}
}
Node topLeft = intersect(quadTree1.topLeft, quadTree2.topLeft);
Node topRight = intersect(quadTree1.topRight, quadTree2.topRight);
Node bottomLeft = intersect(quadTree1.bottomLeft, quadTree2.bottomLeft);
Node bottomRight = intersect(quadTree1.bottomRight, quadTree2.bottomRight);
if (topLeft.isLeaf == true &&
topRight.isLeaf == true &&
bottomLeft.isLeaf == true &&
bottomRight.isLeaf == true &&
topLeft.val == topRight.val &&
topLeft.val == bottomLeft.val &&
topLeft.val == bottomRight.val &&
topLeft.val == bottomRight.val
) {
return new Node(topLeft.val, true);
} else {
return new Node(false, false, topLeft, topRight, bottomLeft, bottomRight);
}
}
}
递归解法。递归最开始应该考虑最后的情况,逐渐往前推动逻辑。