### Python program to find diameter of a binary tree

program to find diameter of a binary tree

The diameter of a binary tree is the length of the longest path between any two nodes in a tree. This path may or may not pass through the root.

Program:
class Node: def __init__(self,key): self.left = None self.right = None self.val = key def printInorder(root): if root: printInorder(root.left) print(root.val,end=" ") printInorder(root.right) def height(node): if node is None: return 0 return 1 + max(height(node.left), height(node.right)) def diameter(root): if root is None: return 0 LH = height(root.left) RH = height(root.right) LD = diameter(root.left) RD = diameter(root.right) return max(LH + RH + 1, max(LD, RD)) if __name__ == '__main__': Root = Node(100) Root.left = Node(200) Root.right = Node(300) Root.left.right = Node(400) Root.left.left = Node(500) Root.right.left = Node(600) Root.right.right = Node(700) print('\n') print('Inorder Traversal of BT is:') printInorder(Root) print('\n') print('Diameter of BT is:{}'.format(diameter(Root))) else: pass

Output :
D:\JB>python 1.py

Inorder Traversal of BT is:
500 200 400 100 600 300 700

Diameter of BT is:5

#### More Articles of abhijeet singh:

Name Views Likes
Python program to find diameter of a binary tree 820 16
Python program to find sum of all leaf nodes of binary tree 357 18
Python program to find height of a tree using recursion 385 16
Python program to find sum of all nodes in a binary tree 385 15
Python program to check whether a given binary tree is complete or not using recursion 322 18
Python program to check whether a binary tree is a full binary tree or not using recursion 428 30
Python program to convert a given binary search tree to doubly linked list 356 20
Python program to find K%u2019th largest element in binary search tree 342 25
Python program to find lowest Common Ancestor in a binary search tree 368 21
Python program to find k-th smallest element in binary search tree 324 17
Python program to find the node having maximum value in a binary search tree 327 19
Python program to find the node having minimum value in a binary search tree 385 18
Python program to construct binary search tree from given preorder traversal 377 18
Python Program to Implement Queue using two Stacks 404 19
Python Program to count and display occurence of a given Character and lines not starting with given Character 350 18
Python Program to find number of alphabets and number of lines in a file 303 17
Python Program to write numbers 1 to 100 in a file 327 17
Python Program to find number of words in the file 299 12
Python Program to create a file,find size of a file and copy content of one file in another 335 22
Python Program to create and delete a File 350 21
Python Program to demonstrate Monkey Patching 317 11
Python Program to find Sum of list (with string types) 344 21
Python program to create a list of tuples from given list having number and its cube in each tuple 435 18
Python Program to find missing number in a given integer array of 1 to 100 477 23
Python Program to perform files compression to tar file and extract a tar file 298 14
Python Program to demonstrate Various list Functions 444 15
Python GUI Registration Form Using Tkinter 915 25
Python Automated tool to convert Plain Text to Formatted text using Pygments 413 21
Python Program to Generate random numbers 327 20
Python Program to Swap two strings 376 20
Python Program to Perform Set Operations 354 22
Python Program to Find the Transpose of a Given Matrix 385 18
Python Program to Perform Addition,Subtraction and Multiplication of two Matrices 2191 23
Python Program to Calculate Permutation(nPr) and Combination(nCr) 712 23
Python Program to Find the Factorial of a Given Number 297 13
Python Program to Print the Fibonacci Sequence ( without Recursion) 367 26
Python Program to Calculate the Perimeter of a Square 373 28