
- 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
Find the largest Perfect Subtree in a given Binary Tree in Python
Suppose we have a given Binary Tree; we have to find the size of largest Perfect sub-tree in that given Binary Tree. As we know the perfect binary tree is a binary tree in which all internal nodes have two children and all leaves are at the identical level.
So, if the input is like
then the output will be 3, and the subtree is
To solve this, we will follow these steps −
Define one block called RetType, this will hold isPerfect, height and rootTree, they are all initially 0
Define a function called get_prefect_subtree(), this takes root
r_type := a new RetType
if root is same as None, then
r_type.isPerfect := True
r_type.height := 0
r_type.rootTree := null
return r_type
left_subtree := get_prefect_subtree(root.left)
right_subtree := get_prefect_subtree(root.right)
if left_subtree is perfect and right_subtree is perfect and height of left_subtree is same as height of right_subtree, then
height of r_type := height of left_subtree + 1
set r_type is perfect
r_type.rootTree := root
return r_type
set r_type is not perfect
r_type.height := maximum of height of left_subtree, height of right_subtree
if height of left_subtree > height of right_subtree, then
r_type.rootTree := left_subtree.rootTree
otherwise,
r_type.rootTree := right_subtree.rootTree
return r_type
Example
Let us see the following implementation to get better understanding −
class TreeNode: def __init__(self, data, left = None, right = None): self.data = data self.left = left self.right = right def print_tree(root): if root is not None: print_tree(root.left) print(root.data, end = ', ') print_tree(root.right) class RetType: def __init__(self): isPerfect = 0 height = 0 rootTree = 0 def get_prefect_subtree(root): r_type = RetType() if (root == None) : r_type.isPerfect = True r_type.height = 0 r_type.rootTree = None return r_type left_subtree = get_prefect_subtree(root.left) right_subtree = get_prefect_subtree(root.right) if (left_subtree.isPerfect and right_subtree.isPerfect and left_subtree.height == right_subtree.height) : r_type.height = left_subtree.height + 1 r_type.isPerfect = True r_type.rootTree = root return r_type r_type.isPerfect = False r_type.height = max(left_subtree.height, right_subtree.height) if (left_subtree.height > right_subtree.height ): r_type.rootTree = left_subtree.rootTree else : r_type.rootTree = right_subtree.rootTree return r_type root = TreeNode(2) root.left = TreeNode(3) root.right = TreeNode(4) root.left.left = TreeNode(5) root.left.right = TreeNode(6) root.right.left = TreeNode(7) res = get_prefect_subtree(root) h = res.height print ("Size: " , pow(2, h) - 1) print ("Tree: ", end = " ") print_tree(res.rootTree)
Input
root = TreeNode(2) root.left = TreeNode(3) root.right = TreeNode(4) root.left.left = TreeNode(5) root.left.right = TreeNode(6) root.right.left = TreeNode(7)
Output
Size: 3 Tree: 5, 3, 6,
- Related Articles
- Find the largest Complete Subtree in a given Binary Tree in Python
- Find the largest Complete Subtree in a given Binary Tree in C++
- Program to find largest binary search subtree from a given tree in Python
- Find the largest BST subtree in a given Binary Tree - Set 1 in C++
- Find largest subtree sum in a tree in C++
- Program to find most frequent subtree sum of a binary tree in Python
- Program to find out the largest sum value of a BST in a given binary tree in Python
- Find sum of all nodes of the given perfect binary tree in C++
- Check if a binary tree is subtree of another binary tree in C++
- C++ Program to Find Size of the Largest Independent Set(LIS) in a Given a Binary Tree
- Python Program To Find the Smallest and Largest Elements in the Binary Search Tree
- Program to find largest sum of any path of a binary tree in Python
- Largest BST in a Binary Tree in C++
- Find the Kth node in the DFS traversal of a given subtree in a Tree in C++
- Program to find the largest sum of the path between two nodes in a binary tree in Python
