http://www.geeksforgeeks.org/find-level-maximum-sum-binary-tree/
http://www.geeksforgeeks.org/maximum-width-of-a-binary-tree/
int maxLevelSum(struct Node * root){ // Base case if (root == NULL) return 0; // Initialize result int result = root->data; // Do Level order traversal keeping track of number // of nodes at every level. queue<Node*> q; q.push(root); while (!q.empty()) { // Get the size of queue when the level order // traversal for one level finishes int count = q.size() ; // Iterate for all the nodes in the queue currently int sum = 0; while (count--) { // Dequeue an node from queue Node *temp = q.front(); q.pop(); // Add this node's value to current sum. sum = sum + temp->data; // Enqueue left and right children of // dequeued node if (temp->left != NULL) q.push(temp->left); if (temp->right != NULL) q.push(temp->right); } // Update the maximum node count value result = max(sum, result); } return result;}
Given a binary tree, write a function to get the maximum width of the given tree. Width of a tree is maximum of widths of all levels.
int maxWidth(struct Node * root){ // Base case if (root == NULL) return 0; // Initialize result int result = 0; // Do Level order traversal keeping track of number // of nodes at every level. queue<Node*> q; q.push(root); while (!q.empty()) { // Get the size of queue when the level order // traversal for one level finishes int count = q.size() ; // Update the maximum node count value result = max(count, result); // Iterate for all the nodes in the queue currently while (count--) { // Dequeue an node from queue Node *temp = q.front(); q.pop(); // Enqueue left and right children of // dequeued node if (temp->left != NULL) q.push(temp->left); if (temp->right != NULL) q.push(temp->right); } } return result;}