
- 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 check whether a binary tree is complete or not in Python
Suppose we have a binary tree; we have to check whether this is a complete binary tree or not. As we know in a complete binary tree the levels are filled with nodes except possibly the last and all nodes in the last level are as far left as possible.
So, if the input is like
then the output will be True
To solve this, we will follow these steps−
q := a double ended queue
insert root at the end of q
flag := False
while q is not empty, do
temp := element after deleting from left of q
if temp is null, then
flag := True
otherwise when flag is set and temp is not null, then
return False
otherwise,
insert left of temp at the end of q
insert right of temp at the end of q
return True
Let us see the following implementation to get better understanding −
Example
from collections import deque 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): q = deque() q.append(root) flag = False while q: temp = q.popleft() if not temp: flag = True elif flag and temp: return False else: q.append(temp.left) q.append(temp.right) return True ob = Solution() root = TreeNode(9) root.left = TreeNode(7) root.right = TreeNode(10) root.left.left = TreeNode(6) root.left.right = TreeNode(8) print(ob.solve(root))
Input
root = TreeNode(9) root.left = TreeNode(7) root.right = TreeNode(10) root.left.left = TreeNode(6) root.left.right = TreeNode(8)
Output
True
- Related Articles
- Program to check whether a binary tree is BST or not in Python
- C++ Program to Check Whether a given Binary Tree is a Full Binary Tree or not
- Program to check whether given tree is symmetric tree or not in Python
- Program to check whether inorder sequence of a tree is palindrome or not in Python
- Program to check whether one tree is subtree of other or not in Python
- Program to check whether a tree is height balanced or not in C++
- A program to check if a binary tree is BST or not in C ?
- C++ program to Check if a Given Binary Tree is an AVL Tree or Not
- C++ Program to Check Whether a Given Tree is Binary Search Tree
- Python program to check whether a list is empty or not?
- C++ Program to Check Whether an Input Binary Tree is the Sub Tree of the Binary Tree
- Python program to check whether a given string is Heterogram or not
- Program to check whether given graph is bipartite or not in Python
- Check if a binary tree is sorted levelwise or not in C++
- How to check whether a binary tree is a valid binary search tree using recursion in C#?

Advertisements