Tuesday, January 1, 2019
558. Quad Tree Intersection
奇怪的题,呵呵
点在于求出4个leaf之后需要看这4个leaf是否需要被合并成一个点
33.33 %
class Solution {
public Node intersect(Node quadTree1, Node quadTree2) {
if (quadTree1.isLeaf) {
return quadTree1.val ? quadTree1 : quadTree2;
}
if (quadTree2.isLeaf) {
return quadTree2.val ? quadTree2 : quadTree1;
}
quadTree1.topLeft = intersect(quadTree1.topLeft, quadTree2.topLeft);
quadTree1.topRight = intersect(quadTree1.topRight, quadTree2.topRight);
quadTree1.bottomLeft = intersect(quadTree1.bottomLeft, quadTree2.bottomLeft);
quadTree1.bottomRight = intersect(quadTree1.bottomRight, quadTree2.bottomRight);
// can we combine it into a leaf
if (quadTree1.topLeft.isLeaf && quadTree1.topRight.isLeaf
&& quadTree1.bottomLeft.isLeaf && quadTree1.bottomRight.isLeaf
&& quadTree1.topLeft.val == quadTree1.topRight.val
&& quadTree1.topLeft.val == quadTree1.bottomLeft.val
&& quadTree1.topLeft.val == quadTree1.bottomRight.val) {
quadTree1.isLeaf = true;
quadTree1.val = quadTree1.topLeft.val;
quadTree1.topLeft = null;
quadTree1.topRight = null;
quadTree1.bottomLeft = null;
quadTree1.bottomRight = null;
}
return quadTree1;
}
}
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment