159. Quad Tree Intersection

题目:

A quadtree is a tree data in which each internal node has exactly four children: topLefttopRightbottomLeft and bottomRight. Quad trees are often used to partition a two-dimensional space by recursively subdividing it into four quadrants or regions.

四叉树是树数据,其中每个内部节点恰好有四个子节点:topLeft,topRight,bottomLeft和bottomRight。四叉树通常用于通过递归地将二维空间细分为四个象限或区域来划分二维空间。

We want to store True/False information in our quad tree. The quad tree is used to represent a N * N boolean grid. For each node, it will be subdivided into four children nodes until the values in the region it represents are all the same. Each node has another two boolean attributes : isLeaf and valisLeaf is true if and only if the node is a leaf node. The val attribute for a leaf node contains the value of the region it represents.

我们想在我们的四叉树中存储真/假信息。四叉树用于表示N * N布尔网格。对于每个节点,它将被细分为四个子节点,直到它所代表的区域中的值都相同。每个节点都有另外两个布尔属性:isLeaf和val。当且仅当节点是叶节点时,isLeaf才为真。叶节点的val属性包含它所代表的区域的值。

For example, below are two quad trees A and B:

A:
+-------+-------+   T: true
|       |       |   F: false
|   T   |   T   |
|       |       |
+-------+-------+
|       |       |
|   F   |   F   |
|       |       |
+-------+-------+
topLeft: T
topRight: T
bottomLeft: F
bottomRight: F

B:               
+-------+---+---+
|       | F | F |
|   T   +---+---+
|       | T | T |
+-------+---+---+
|       |       |
|   T   |   F   |
|       |       |
+-------+-------+
topLeft: T
topRight:
     topLeft: F
     topRight: F
     bottomLeft: T
     bottomRight: T
bottomLeft: T
bottomRight: F

 

Your task is to implement a function that will take two quadtrees and return a quadtree that represents the logical OR (or union) of the two trees.

您的任务是实现一个函数,该函数将采用两个四叉树并返回表示两棵树的逻辑OR(或并集)的四叉树。

A:                 B:                 C (A or B):
+-------+-------+  +-------+---+---+  +-------+-------+
|       |       |  |       | F | F |  |       |       |
|   T   |   T   |  |   T   +---+---+  |   T   |   T   |
|       |       |  |       | T | T |  |       |       |
+-------+-------+  +-------+---+---+  +-------+-------+
|       |       |  |       |       |  |       |       |
|   F   |   F   |  |   T   |   F   |  |   T   |   F   |
|       |       |  |       |       |  |       |       |
+-------+-------+  +-------+-------+  +-------+-------+

Note:

  1. Both A and B represent grids of size N * N.A和B都表示大小为N * N的网格。
  2. N is guaranteed to be a power of 2.N保证是2的幂。
  3. If you want to know more about the quad tree, you can refer to its wiki.如果您想了解有关四叉树的更多信息,可以参考其维基。
  4. The logic OR operation is defined as this: "A or B" is true if A is true, or if B is true, or if both A and B are true.逻辑OR运算定义如下:如果A为真,或者如果B为真,或者如果A和B都为真,则“A或B”为真。

解答:

 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)
26             return quadTree1.val? quadTree1:quadTree2;
27         if(quadTree2.isLeaf)
28             return quadTree2.val? quadTree2:quadTree1;
29         
30         quadTree1.topLeft=intersect(quadTree1.topLeft,quadTree2.topLeft);
31         quadTree1.topRight=intersect(quadTree1.topRight,quadTree2.topRight);
32         quadTree1.bottomLeft=intersect(quadTree1.bottomLeft,quadTree2.bottomLeft);
33         quadTree1.bottomRight=intersect(quadTree1.bottomRight,quadTree2.bottomRight);
34         
35         if(quadTree1.topLeft.isLeaf && quadTree1.topRight.isLeaf 
36           && quadTree1.bottomLeft.isLeaf && quadTree1.bottomRight.isLeaf
37           && quadTree1.topLeft.val==quadTree1.topRight.val
38           && quadTree1.topRight.val==quadTree1.bottomLeft.val
39           && quadTree1.bottomLeft.val==quadTree1.bottomRight.val){  //叶子节点值四个值全部相同就合并成一个
40             quadTree1.isLeaf=true;
41             quadTree1.val=quadTree1.topLeft.val;            
42         }
43         
44         return quadTree1;
45     }
46 }

详解:

true || x  始终是true

false || x  始终是x

所以如果quadTree1 是true,则返回quadTree1,如果quadTree1 是false,则返回quadTree2

posted @ 2018-09-11 20:27  chan_ai_chao  阅读(114)  评论(0编辑  收藏  举报