
- 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 largest binary search subtree from a given tree in Python
Suppose we have a binary tree, we have to find the largest subtree (with maximum number of nodes) as binary search tree.
So, if the input is like
then the output will be
To solve this, we will follow these steps −
- max_size := [0]
- max_node := [null]
- Define a function traverse() . This will take node
- if node is null, then
- return null
- left := traverse(left of node)
- right := traverse(right of node)
- lst := left + [value of node] + right
- if lst is sorted, then
- if max_size[0] < size of lst, then
- max_size[0] := size of lst
- max_node[0] := node
- if max_size[0] < size of lst, then
- return lst
- traverse(root)
- From the main method return max_node[0]
Example (Python)
Let us see the following implementation to get better understanding −
class TreeNode: def __init__(self, data, left = None, right = None): self.val = data self.left = left self.right = right def print_tree(root): if root is not None: print_tree(root.left) print(root.val, end = ', ') print_tree(root.right) class Solution: def solve(self, root): max_size = [0] max_node = [None] def traverse(node): if not node: return [] left = traverse(node.left) right = traverse(node.right) lst = left + [node.val] + right if sorted(lst) == lst: if max_size[0] < len(lst): max_size[0] = len(lst) max_node[0] = node return lst traverse(root) return max_node[0] ob = Solution() root = TreeNode(12) root.left = TreeNode(3) root.right = TreeNode(5) root.right.left = TreeNode(4) root.right.right = TreeNode(6) print_tree(ob.solve(root))
Input
root = TreeNode(12) root.left = TreeNode(3) root.right = TreeNode(5) root.right.left = TreeNode(4) root.right.right = TreeNode(6)
Output
4, 5, 6,
- Related Articles
- Find the largest Perfect Subtree in a given Binary Tree in Python
- Find the largest Complete Subtree in a given Binary Tree in Python
- Find the largest Complete Subtree in a given Binary Tree in C++
- Find the largest BST subtree in a given Binary Tree - Set 1 in C++
- Python Program To Find the Smallest and Largest Elements in the Binary Search Tree
- Program to find most frequent subtree sum of a binary tree in Python
- Construct a Binary Search Tree from given postorder in Python
- Find largest subtree sum in a tree in C++
- C++ Program to Check Whether a Given Tree is Binary Search Tree
- Program to find out the largest sum value of a BST in a given binary tree in Python
- Python Program to Sort using a Binary Search Tree
- Program to find largest sum of any path of a binary tree in Python
- Program to find the kth smallest element in a Binary Search Tree in Python
- C++ Program to Find Size of the Largest Independent Set(LIS) in a Given a Binary Tree
- Construct Binary Search Tree from Preorder Traversal in Python

Advertisements