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