Skip to content

Instantly share code, notes, and snippets.

@kanrourou
Last active October 17, 2017 04:32
Node* createTree(vector<vector<int>>& matrix, int row1, int row2, int col1, int col2)
{
if(row1 > row2 || col1 > col2)return nullptr;
if(row1 == row2 && col1 == col2)return new Node(matrix[row1][col1]);
int rMid = row1 + (row2 - row1) / 2, cMid = col1 + (col2 - col1) / 2;
auto topL = createTree(matrix, row1, rMid, col1, cMid);
auto topR = createTree(matrix, row1, rMid, cMid + 1, col2);
auto botL = createTree(matrix, rMid + 1, row2, col1, cMid);
auto botR = createTree(matrix, rMid + 1, row2, cMid + 1, col2);
Node* node = new Node(val(topL) + val(topR) + val(botL) + val(botR));
node->children[0] = topL;
node->children[1] = topR;
node->children[2] = botL;
node->children[3] = botR;
return node;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment