BackhardTrees

Maximum Tree Width Solution

Problem Statement

Given a tree with n nodes, each having a unique value, find the maximum width of the tree, where the width is defined as the maximum number of nodes at any level of the tree. The tree is represented as an adjacency list or array, and edges are given as pairs of node values.

Example 1
Input
Tree: [1, 2, 3, 4, 5, 6, 7], Edges: [(1, 2), (1, 3), (2, 4), (2, 5), (3, 6), (3, 7)]
Output
4

Explanation: The tree has the following structure: 1 / \ 2 3 / \ / \ 4 5 6 7. The maximum width occurs at level 3 with 4 nodes.

Constraints

  • 1 <= n <= 10^4
  • Each node value is unique and between 1 and n
  • The tree is connected and undirected
Live Compiler
Loading...
Test Cases & Output
🔒 Sign up to run your code

🚀 Practice this problem

Run code, get AI hints & track streak

Sign Up Free