
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Maximum width of a binary tree in C++
Problem statement
Given a binary tree, write a function to get the maximum width of the given tree. The width of a tree is the maximum of widths of all levels.
Consider below tree −
10 / \ 7 4 / \ \ 9 2 1 / \ 2 5 1. Width at level 1: 1 2. Width at level 2: 2 3. Width at level 3: 3 4. Width at level 4: 2 For above tree answer is 3.
Algorithm
1. Use level order traversal to find the answer
Example
#include <bits/stdc++.h> using namespace std; struct node { public: int data; node* left; node* right; }; int getWidth(node* root, int level); int height(node* node); node* newNode(int data); int getMaxWidth(node* root){ int maxWidth = 0; int width; int h = height(root); int i; for (i = 1; i <= h; ++i) { width = getWidth(root, i); if (width > maxWidth) { maxWidth = width; } } return maxWidth; } int getWidth(node* root, int level){ if (root == NULL) { return 0; } if (level == 1) { return 1; } else if (level > 1) { return getWidth(root->left, level - 1) + getWidth(root->right, level - 1); } } int height(node* node){ if (node == NULL) { return 0; } int lHeight = height(node->left); int rHeight = height(node->right); return (lHeight > rHeight)? (lHeight + 1): (rHeight + 1); } node* newNode(int data){ node* Node = new node(); Node->data = data; Node->left = NULL; Node->right = NULL; return(Node); } int main(){ node *root = newNode(10); root->left = newNode(7); root->right = newNode(4); root->left->left = newNode(9); root->left->right = newNode(2); root->right->right = newNode(1); root->right->right->left = newNode(2); root->right->right->right = newNode(5); cout<<"Maximum width = " << getMaxWidth(root) << endl; return 0; }
Output
When you compile and execute the above program. It generates the following output −
Maximum width = 3
- Related Articles
- Maximum Width of Binary Tree in C++
- Program to find the maximum width of a binary tree in Python
- Maximum Binary Tree in C++
- Maximum Level Sum of a Binary Tree in C++
- Maximum Binary Tree II in C++
- Maximum Path Sum in a Binary Tree in C++
- Maximum spiral sum in Binary Tree in C++
- Maximum Sum BST in Binary Tree in C++
- Maximum Depth of Binary Tree in Python
- Find maximum vertical sum in binary tree in C++
- Find maximum level product in Binary Tree in C++
- Maximum parent children sum in Binary tree in C++
- Find maximum (or minimum) in Binary Tree in C++
- Find maximum level sum in Binary Tree in C++
- Maximum Consecutive Increasing Path Length in Binary Tree in C++

Advertisements