A quadtree is a tree data in which each internal node has exactly four children: topLeft
, topRight
, bottomLeft
and bottomRight
. Quad trees are often used to partition a two-dimensional space by recursively subdividing it into four quadrants or regions.html
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 val
. isLeaf
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.node
For example, below are two quad trees A and B:ide
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.函數
A: B: C (A or B): +-------+-------+ +-------+---+---+ +-------+-------+ | | | | | F | F | | | | | T | T | | T +---+---+ | T | T | | | | | | T | T | | | | +-------+-------+ +-------+---+---+ +-------+-------+ | | | | | | | | | | F | F | | T | F | | T | F | | | | | | | | | | +-------+-------+ +-------+-------+ +-------+-------+
Note:post
A
and B
represent grids of size N * N
.N
is guaranteed to be a power of 2.A is true
, or if B is true
, or if both A and B are true
.
這道題又是一道四叉樹的題,說是給了咱們兩個四叉樹,而後讓咱們將二棵樹相交造成了一棵四叉樹,相交的機制採用的是或,即每一個自區域相‘或’,題目中給的例子很好的說明了一些相‘或’的原則,好比咱們看A和B中的右上結點,咱們發現A樹的右上結點已是一個值爲true的葉結點,而B的右上結點仍是一個子樹,那麼此時不論子樹裏有啥內容,咱們相交後的樹的右上結點應該跟A樹的右上結點保持一致,假如A樹的右上結點值是false的話,相‘或’起不到任何做用,那麼相交後的樹的右上結點應該跟B樹的右上結點保持一致。那麼咱們能夠概括出,只有某一個結點是葉結點了,咱們看其值,若是是true,則相交後的結點和此結點保持一致,不然跟另外一個結點保持一致。比較麻煩的狀況是當兩個結點都不是葉結點的狀況,此時咱們須要對相對應的四個子結點分別調用遞歸函數,調用以後還須要進行進一步處理,由於一旦四個子結點的值相同,且都是葉結點的話,那麼此時應該合併爲一個大的葉結點,參見代碼以下:this
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; Node *tl = intersect(quadTree1->topLeft, quadTree2->topLeft); Node *tr = intersect(quadTree1->topRight, quadTree2->topRight); Node *bl = intersect(quadTree1->bottomLeft, quadTree2->bottomLeft); Node *br = intersect(quadTree1->bottomRight, quadTree2->bottomRight); if (tl->val == tr->val && tl->val == bl->val && tl->val == br->val && tl->isLeaf && tr->isLeaf && bl->isLeaf && br->isLeaf) { return new Node(tl->val, true, NULL, NULL, NULL, NULL); } else { return new Node(false, false, tl, tr, bl, br); } } };
相似題目:url
參考資料:code
https://leetcode.com/problems/quad-tree-intersection/orm
https://leetcode.com/problems/quad-tree-intersection/discuss/152730/C%2B%2B-10-line-beat-100
https://leetcode.com/problems/quad-tree-intersection/discuss/157532/Java-concise-code-beat-100