{ "data": { "question": { "questionId": "773", "questionFrontendId": "558", "boundTopicId": null, "title": "Logical OR of Two Binary Grids Represented as Quad-Trees", "titleSlug": "logical-or-of-two-binary-grids-represented-as-quad-trees", "content": "
A Binary Matrix is a matrix in which all the elements are either 0 or 1.
\n\nGiven quadTree1
and quadTree2
. quadTree1
represents a n * n
binary matrix and quadTree2
represents another n * n
binary matrix.
Return a Quad-Tree representing the n * n
binary matrix which is the result of logical bitwise OR of the two binary matrixes represented by quadTree1
and quadTree2
.
Notice that you can assign the value of a node to True or False when isLeaf
is False, and both are accepted in the answer.
A Quad-Tree is a tree data structure in which each internal node has exactly four children. Besides, each node has two attributes:
\n\nval
: True if the node represents a grid of 1's or False if the node represents a grid of 0's.isLeaf
: True if the node is leaf node on the tree or False if the node has the four children.\nclass Node {\n public boolean val;\n public boolean isLeaf;\n public Node topLeft;\n public Node topRight;\n public Node bottomLeft;\n public Node bottomRight;\n}\n\n
We can construct a Quad-Tree from a two-dimensional area using the following steps:
\n\n1's
or all 0's
) set isLeaf
True and set val
to the value of the grid and set the four children to Null and stop.isLeaf
to False and set val
to any value and divide the current grid into four sub-grids as shown in the photo.If you want to know more about the Quad-Tree, you can refer to the wiki.
\n\nQuad-Tree format:
\n\nThe input/output represents the serialized format of a Quad-Tree using level order traversal, where null
signifies a path terminator where no node exists below.
It is very similar to the serialization of the binary tree. The only difference is that the node is represented as a list [isLeaf, val]
.
If the value of isLeaf
or val
is True we represent it as 1 in the list [isLeaf, val]
and if the value of isLeaf
or val
is False we represent it as 0.
\n
Example 1:
\n \n\nInput: quadTree1 = [[0,1],[1,1],[1,1],[1,0],[1,0]]\n, quadTree2 = [[0,1],[1,1],[0,1],[1,1],[1,0],null,null,null,null,[1,0],[1,0],[1,1],[1,1]]\nOutput: [[0,0],[1,1],[1,1],[1,1],[1,0]]\nExplanation: quadTree1 and quadTree2 are shown above. You can see the binary matrix which is represented by each Quad-Tree.\nIf we apply logical bitwise OR on the two binary matrices we get the binary matrix below which is represented by the result Quad-Tree.\nNotice that the binary matrices shown are only for illustration, you don't have to construct the binary matrix to get the result tree.\n\n\n\n
Example 2:
\n\n\nInput: quadTree1 = [[1,0]], quadTree2 = [[1,0]]\nOutput: [[1,0]]\nExplanation: Each tree represents a binary matrix of size 1*1. Each matrix contains only zero.\nThe resulting matrix is of size 1*1 with also zero.\n\n\n
\n
Constraints:
\n\nquadTree1
and quadTree2
are both valid Quad-Trees each representing a n * n
grid.n == 2x
where 0 <= x <= 9
.Compiled with clang 11
using the latest C++ 20 standard.
Your code is compiled with level two optimization (-O2
). AddressSanitizer is also enabled to help detect out-of-bounds and use-after-free bugs.
Most standard library headers are already included automatically for your convenience.
\"], \"java\": [\"Java\", \"OpenJDK 17
. Java 8 features such as lambda expressions and stream API can be used.
Most standard library headers are already included automatically for your convenience.
\\r\\nIncludes Pair
class from https://docs.oracle.com/javase/8/javafx/api/javafx/util/Pair.html.
Python 2.7.12
.
Most libraries are already imported automatically for your convenience, such as array, bisect, collections. If you need more libraries, you can import it yourself.
\\r\\n\\r\\nFor Map/TreeMap data structure, you may use sortedcontainers library.
\\r\\n\\r\\nNote that Python 2.7 will not be maintained past 2020. For the latest Python, please choose Python3 instead.
\"], \"csharp\": [\"C#\", \"\"], \"javascript\": [\"JavaScript\", \"Node.js 16.13.2
.
Your code is run with --harmony
flag, enabling new ES6 features.
lodash.js library is included by default.
\\r\\n\\r\\nFor Priority Queue / Queue data structures, you may use 5.3.0 version of datastructures-js/priority-queue and 4.2.1 version of datastructures-js/queue.
\"], \"ruby\": [\"Ruby\", \"Ruby 3.1
Some common data structure implementations are provided in the Algorithms module: https://www.rubydoc.info/github/kanwei/algorithms/Algorithms
\"], \"swift\": [\"Swift\", \"Swift 5.5.2
.
Go 1.21
Support https://godoc.org/github.com/emirpasic/gods@v1.18.1 library.
\"], \"python3\": [\"Python3\", \"Python 3.10
.
Most libraries are already imported automatically for your convenience, such as array, bisect, collections. If you need more libraries, you can import it yourself.
\\r\\n\\r\\nFor Map/TreeMap data structure, you may use sortedcontainers library.
\"], \"scala\": [\"Scala\", \"Scala 2.13.7
.
Kotlin 1.9.0
.
PHP 8.1
.
With bcmath module
\"], \"typescript\": [\"Typescript\", \"TypeScript 5.1.6, Node.js 16.13.2
.
Your code is run with --harmony
flag, enabling new ES2022 features.
lodash.js library is included by default.
\"]}", "libraryUrl": null, "adminUrl": null, "challengeQuestion": null, "__typename": "QuestionNode" } } }