
- 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
Balanced Binary Tree in Python
In a binary tree, each node contains two children, i.e left child and right child. Let us suppose we have a binary tree and we need to check if the tree is balanced or not. A Binary tree is said to be balanced if the difference of height of left subtree and right subtree is less than or equal to '1'.
Example
Input-1:
Output:
True
Explanation:
The given binary tree is [1,2,3, NULL, NULL, 6, 7]. The height difference of its left subtree and right subtree is equal to '1', thus it is a height Balanced tree.
Input-2:
Output:
False
Explanation:
The given binary tree is [1,2,3,4, NULL, NULL,NULL,5]. The height difference of its left subtree and right subtree is greater than '1', thus it is not a height balanced tree.
Approach to Solve this Problem
The recursive approach to solve this problem is to find the height of the left subtree and the right subtree and then check if (height(leftsubstree) - height(rightsubtree) <= 1) and return True or False accordingly. Then, we will check recursively for each node of the binary tree.
- Take input of nodes of a Binary Tree.
- Define a function to find the height of the tree.
- A Boolean function to check recursively if the height difference of left subtree and right subtree is not more than '1', then return True.
- Return the Result.
Example
class treenode: def __init__(self, data): self.data = data self.left = self.right = None # funtion to find the height of the left subtree and right subtree class height: def __init__(self): self.height = 0 # function to check if the tree is balanced or not def isBalanced(root): lh = height() rh = height() if root is None: return True return ( (abs(lh.height - rh.height) <= 1) and isBalanced(root.left) and isBalanced(root.right) ) root = treenode(1) root.left = treenode(2) root.right = treenode(3) root.left.left = None root.left.right = None root.right.left = treenode(6) root.right.right = treenode(7) if isBalanced(root): print("Balanced") else: print("Not Balanced")
Running the above code will generate the output as,
Output
Balanced
The given binary tree [1, 2, 3, NULL, NULL, 6, 7]. The height difference of its left subtree and right subtree is equal to '1', thus it is a height Balanced tree.
- Related Articles
- Check if a given Binary Tree is height balanced like a Red-Black Tree in Python
- Invert Binary Tree in Python
- Check if a given Binary Tree is height balanced like a Red-Black Tree in C++
- Validate Binary Search Tree in Python
- Diameter of Binary Tree in Python
- Binary Tree Inorder Traversal in Python
- Binary Tree Preorder Traversal in Python
- Binary Tree Coloring Game in Python
- Binary Tree Postorder Traversal in Python
- Maximum Depth of Binary Tree in Python
- Construct String from Binary Tree in Python
- Binary Tree Maximum Path Sum in Python
- Binary Tree Postorder Traversal in Python Programming
- Path In Zigzag Labelled Binary Tree in Python
- Binary Tree to Binary Search Tree Conversion in C++
