
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Program to find the maximum width of a binary tree in Python
Suppose we have a binary tree, we have to find the maximum width of any level in the tree. Here the width of a level is the number of nodes that can hold between the leftmost node and the rightmost node.
So, if the input is like
then the output will be 2
To solve this, we will follow these steps−
create a map d, to hold minimum and maximum values, minimum is initially infinity and maximum is 0
Define a function dfs() . This will take root, pos := 0, depth := 0
if root is null, then o return
d[depth, 0] = minimum of d[depth,0] and pos
d[depth, 1] = maximum of d[depth,1] and pos
dfs(left of node, 2*pos, depth+1)
dfs(right of node, 2*pos+1, depth+1)
From the main method, do the following−
dfs(root)
mx:= 0
for each min-max pairs in list of all values of d, do
left := min, right := max
mx:= maximum of mx, right-lelf+1
return mx
Let us see the following implementation to get better understanding −
Example
Live Demo
from collections import defaultdict class TreeNode: def __init__(self, data, left = None, right = None): self.data = data self.left = left self.right = right class Solution: def solve(self, root): d=defaultdict(lambda: [1e9,0]) def dfs(node, pos=0, depth=0): if not node: return d[depth][0]=min(d[depth][0],pos) d[depth][1]=max(d[depth][1],pos) dfs(node.left,2*pos,depth+1) dfs(node.right,2*pos+1,depth+1) dfs(root) mx=0 for interval in d.values(): l,r=interval mx=max(mx,r-l+1) return mx ob = Solution() root = TreeNode(5) root.left = TreeNode(1) root.right = TreeNode(9) root.right.left = TreeNode(7) root.right.right = TreeNode(10) root.right.left.left = TreeNode(6) root.right.left.right = TreeNode(8) print(ob.solve(root))
Input
root = TreeNode(5) root.left = TreeNode(1) root.right = TreeNode(9) root.right.left = TreeNode(7) root.right.right = TreeNode(10) root.right.left.left = TreeNode(6) root.right.left.right = TreeNode(8)
Output
2
- Related Articles
- Maximum width of a binary tree in C++
- Maximum Width of Binary Tree in C++
- Program to find maximum width ramp in Python
- Maximum Depth of Binary Tree in Python
- Program to find top view of a binary tree in Python
- Python Program to Find the Sum of All Nodes in a Binary Tree
- Program to find sibling value of a binary tree node in Python
- Program to find number of only child in a binary tree in python
- Binary Tree Maximum Path Sum in Python
- Program to find longest even value path of a binary tree in Python
- Program to find most frequent subtree sum of a binary tree in Python
- Program to invert a binary tree in Python
- Program to find second deepest node in a binary tree in python
- Program to find largest sum of any path of a binary tree in Python
- Program to find length of longest alternating path of a binary tree in python
