int right_sum(struct node *node){ if (node == NULL){ return 0; } else{ int lsum = right_sum(node->left); int rsum = right_sum(node->right); return node->data + lsum + rsum; } }
int right_sum(struct node *head) { if (head->left != NULL) rsum = rsum + head->left->data; if (head->right != NULL) rsum = rsum+ head->right->data; if (head->left != NULL) right_sum(head->left); if (head->right != NULL) right_sum(head->right); return rsum; }
int solution(vector<int> &A) { //code in C++98 int N = A.size(); //pass1: left sums, going -> int cur_sum = 0; vector<int> left_sum(N,0); for(int i = 1; i < N-1; i++) { cur_sum = max(0, cur_sum + A[i]); left_sum[i] = cur_sum; } //pass2: right sums, going <- cur_sum = 0; vector<int> right_sum(N,0); for(int i = N-2; i > 0; i--) { cur_sum = max(0, cur_sum + A[i]); right_sum[i] = cur_sum; } //pass3: going througn all pairs of left and right sums int max_double_slice = 0; for(int i = 0; i < N-2; i++) { max_double_slice = max(max_double_slice,left_sum[i]+right_sum[i+2]); } return max_double_slice; }
int get_right_subtree_sum(struct node *root){ int sum=0; if (root == NULL) return 0; if (root->right != NULL) { sum = right_sum(root->right); sum = sum + root->right->data; } return sum; }
//------------------------------------------------------------ // find the best location to divide this cube into two volumes //------------------------------------------------------------ int t_color_cube::find_best_cut( int axis, t_color_sum& best_sum, __int64& best_delta ) const { t_color_sum left_sum; t_color_sum right_sum( *this ); __int64 delta; int color; int best_color = 256; t_color_count_list::const_iterator index; best_delta = 0; index = m_pixels[axis].begin(); while (index != m_pixels[axis].end()) { color = (*index)->color[axis] + 1; while (index != m_pixels[axis].end() && (*index)->color[axis] < color) { left_sum += **index; right_sum -= **index; index++; } if (right_sum.get_count() == 0) break; delta = left_sum.get_delta() + right_sum.get_delta(); if (delta < best_delta ) { best_delta = delta; best_sum = right_sum; best_color = color; } } return best_color; }
int get_right_subtree_sum(struct node *root){ if (!root){ return -1; } return right_sum(root->right); }