1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
| int maxSearchTreeSize(node *head) { return process4(head).maxSearchSize; }
struct returnData4 { int maxSearchSize; bool isAllSearch; int max; int min; returnData4(int maxSearchSize, bool isAllSearch, int max, int min) :maxSearchSize(maxSearchSize), isAllSearch(isAllSearch), max(max), min(min){} }; returnData4 process4(node *head) { if (head == NULL) return returnData4(0, true, INT_MIN, INT_MAX); returnData4 leftData = process4(head->left); returnData4 rightData = process4(head->right); int p1 = leftData.maxSearchSize; int p2 = rightData.maxSearchSize; int p3 = INT_MIN; if (leftData.isAllSearch && rightData.isAllSearch && head->val > leftData.max && head->val < rightData.min) { p3 = p1 + p2 + 1; } int maxSearchSize = max(max(p1, p2), p3); bool isAllSearch = (p3 == maxSearchSize) ? true : false; int maxm = max(max(leftData.max, rightData.max), head->val); int minm = min(min(leftData.min, rightData.min), head->val); return returnData4(maxSearchSize, isAllSearch, maxm, minm); }
|