give n n matrix grid 0s 1s we want represent grid quadtreereturn root QuadTree represent gridNotice assign value node true False isLeaf False accept answerA QuadTree tree data structure internal node exactly child besides node attributesval true node represents grid 1s false node represents grid 0sisLeaf true node leaf node tree false node childrenclass Node public boolean val public boolean isLeaf public Node topLeft public Node topright public Node bottomleft public Node bottomrightwe construct QuadTree twodimensional area follow stepsIf current grid value ie 1s 0s set isLeaf true set val value grid set child Null stopIf current grid different value set isLeaf False set val value divide current grid subgrid show photorecurse child proper subgridIf want know QuadTree refer wikiquadtree formatthe output represent serialized format QuadTree level order traversal null signify path terminator node exist belowIt similar serialization binary tree the difference node represent list isLeaf valif value isLeaf val true represent 1 list isLeaf val value isLeaf val False represent 0 example 1input grid 0110output 0110111110explanation the explanation example show belownotice 0 represnt false 1 represent true photo represent QuadTreeExample 2input grid 1111000011110000111111111111111111110000111100001111000011110000output 0111011110nullnullnullnull10101111explanation all value grid we divide grid subgridsthe topleft bottomleft bottomright valuethe topright different value divide 4 subgrid valueExplanation show photo Constraintsn gridlength gridilengthn 2x 0 x 6