Skip to content

Commit 266b303

Browse files
committed
Solve problem Quad Tree Intersection
1 parent 588158f commit 266b303

File tree

4 files changed

+115
-0
lines changed

4 files changed

+115
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,7 @@ All solutions will be accepted!
217217
|872|[Leaf Similar Trees](https://leetcode-cn.com/problems/leaf-similar-trees/description/)|[java/py/js](./algorithms/LeafSimilarTrees)|Easy|
218218
|427|[Construct Quad Tree](https://leetcode-cn.com/problems/construct-quad-tree/description/)|[java/py](./algorithms/ConstructQuadTree)|Easy|
219219
|458|[Poor Pigs](https://leetcode-cn.com/problems/poor-pigs/description/)|[java/py/js](./algorithms/PoorPigs)|Easy|
220+
|558|[Quad Tree Intersection](https://leetcode-cn.com/problems/quad-tree-intersection/description/)|[java/py](./algorithms/QuadTreeIntersection)|Easy|
220221

221222
# Database
222223
|#|Title|Solution|Difficulty|
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Quad Tree Intersection
2+
This problem is easy to solve by recursive
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/*
2+
// Definition for a QuadTree node.
3+
class Node {
4+
public boolean val;
5+
public boolean isLeaf;
6+
public Node topLeft;
7+
public Node topRight;
8+
public Node bottomLeft;
9+
public Node bottomRight;
10+
11+
public Node() {}
12+
13+
public Node(boolean _val,boolean _isLeaf,Node _topLeft,Node _topRight,Node _bottomLeft,Node _bottomRight) {
14+
val = _val;
15+
isLeaf = _isLeaf;
16+
topLeft = _topLeft;
17+
topRight = _topRight;
18+
bottomLeft = _bottomLeft;
19+
bottomRight = _bottomRight;
20+
}
21+
};
22+
*/
23+
class Solution {
24+
public Node intersect(Node quadTree1, Node quadTree2) {
25+
if (quadTree1.isLeaf && quadTree2.isLeaf) {
26+
return new Node(quadTree1.val || quadTree2.val, true, null, null, null, null);
27+
} else if (!quadTree1.isLeaf && !quadTree2.isLeaf) {
28+
Node topLeft = intersect(quadTree1.topLeft, quadTree2.topLeft),
29+
topRight = intersect(quadTree1.topRight, quadTree2.topRight),
30+
bottomLeft = intersect(quadTree1.bottomLeft, quadTree2.bottomLeft),
31+
bottomRight = intersect(quadTree1.bottomRight, quadTree2.bottomRight);
32+
33+
boolean val = topLeft.val;
34+
boolean isLeaf = topLeft.isLeaf && topRight.isLeaf && bottomLeft.isLeaf && bottomRight.isLeaf &&
35+
val == topRight.val && val == bottomLeft.val && val == bottomRight.val;
36+
return new Node(
37+
val,
38+
isLeaf,
39+
isLeaf ? null : topLeft,
40+
isLeaf ? null : topRight,
41+
isLeaf ? null : bottomLeft,
42+
isLeaf ? null : bottomRight
43+
);
44+
} else {
45+
Node node = quadTree1.isLeaf ? quadTree1 : quadTree2,
46+
node1 = quadTree1.isLeaf ? quadTree2 : quadTree1;
47+
return new Node(
48+
quadTree1.val || quadTree2.val,
49+
node.val,
50+
node.val ? null : node1.topLeft,
51+
node.val ? null : node1.topRight,
52+
node.val ? null : node1.bottomLeft,
53+
node.val ? null : node1.bottomRight
54+
);
55+
}
56+
}
57+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
"""
2+
# Definition for a QuadTree node.
3+
class Node(object):
4+
def __init__(self, val, isLeaf, topLeft, topRight, bottomLeft, bottomRight):
5+
self.val = val
6+
self.isLeaf = isLeaf
7+
self.topLeft = topLeft
8+
self.topRight = topRight
9+
self.bottomLeft = bottomLeft
10+
self.bottomRight = bottomRight
11+
"""
12+
class Solution(object):
13+
def intersect(self, quadTree1, quadTree2):
14+
"""
15+
:type quadTree1: Node
16+
:type quadTree2: Node
17+
:rtype: Node
18+
"""
19+
if quadTree1.isLeaf and quadTree2.isLeaf:
20+
return Node(quadTree1.val or quadTree2.val, True, None, None, None, None)
21+
elif quadTree1.isLeaf and not quadTree2.isLeaf:
22+
val = quadTree1.val or quadTree2.val
23+
return Node(
24+
val,
25+
quadTree1.val,
26+
None if quadTree1.val else quadTree2.topLeft,
27+
None if quadTree1.val else quadTree2.topRight,
28+
None if quadTree1.val else quadTree2.bottomLeft,
29+
None if quadTree1.val else quadTree2.bottomRight
30+
)
31+
elif not quadTree1.isLeaf and quadTree2.isLeaf:
32+
val = quadTree1.val or quadTree2.val
33+
return Node(
34+
val,
35+
quadTree2.val,
36+
None if quadTree2.val else quadTree1.topLeft,
37+
None if quadTree2.val else quadTree1.topRight,
38+
None if quadTree2.val else quadTree1.bottomLeft,
39+
None if quadTree2.val else quadTree1.bottomRight
40+
)
41+
else:
42+
topLeft = self.intersect(quadTree1.topLeft, quadTree2.topLeft)
43+
topRight = self.intersect(quadTree1.topRight, quadTree2.topRight)
44+
bottomLeft = self.intersect(quadTree1.bottomLeft, quadTree2.bottomLeft)
45+
bottomRight = self.intersect(quadTree1.bottomRight, quadTree2.bottomRight)
46+
isLeaf = topLeft.isLeaf == topRight.isLeaf == bottomLeft.isLeaf == bottomRight.isLeaf == True and topLeft.val == topRight.val == bottomLeft.val == bottomRight.val
47+
return Node(
48+
topLeft.val,
49+
isLeaf,
50+
topLeft if not isLeaf else None,
51+
topRight if not isLeaf else None,
52+
bottomLeft if not isLeaf else None,
53+
bottomRight if not isLeaf else None
54+
)
55+

0 commit comments

Comments
 (0)