Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@kartikkukreja
Last active August 29, 2015 14:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kartikkukreja/6078cb742ab9cbb4f936 to your computer and use it in GitHub Desktop.
Save kartikkukreja/6078cb742ab9cbb4f936 to your computer and use it in GitHub Desktop.
BRCKTS segment tree node
struct SegmentTreeNode {
int unmatchedOpenParans, unmatchedClosedParans;
void assignLeaf(char paranthesis) {
if (paranthesis == '(')
unmatchedOpenParans = 1, unmatchedClosedParans = 0;
else
unmatchedOpenParans = 0, unmatchedClosedParans = 1;
}
void merge(SegmentTreeNode& left, SegmentTreeNode& right) {
int newMatches = min(left.unmatchedOpenParans, right.unmatchedClosedParans);
unmatchedOpenParans = right.unmatchedOpenParans + left.unmatchedOpenParans - newMatches;
unmatchedClosedParans = left.unmatchedClosedParans + right.unmatchedClosedParans - newMatches;
}
bool getValue() {
return unmatchedOpenParans == 0 && unmatchedClosedParans == 0;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment