
- 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 all leaves are at same level or not in Python
Suppose we have a binary tree; we have to check whether all leaves are at the same level or not.
So, if the input is like
then the output will be True
To solve this, we will follow these steps −
Define a function dfs() . This will take root, d
if root is not null, then
if left of root is null and right of root is null, then
insert d at the end of depth
otherwise,
dfs(left of root, d + 1)
dfs(right of root, d + 1)
From the main method, do the following −
depth := a new list
dfs(root, 0)
return true when depth has only one value
Let us see the following implementation to get better understanding −
Example
class TreeNode: def __init__(self, value): self.val = value self.left = None self.right = None class Solution: def solve(self, root): self.depth = [] self.dfs(root, 0) return len(set(self.depth)) == 1 def dfs(self, root, depth): if root: if not root.left and not root.right: self.depth.append(depth) else: self.dfs(root.left, depth + 1) self.dfs(root.right, depth + 1) ob = Solution() root = TreeNode(5) root.left = TreeNode(4) root.left.left = TreeNode(2) root.right = TreeNode(10) root.right.left = TreeNode(7) root.right.right = TreeNode(15) print(ob.solve(root))
Input
root = TreeNode(5) root.left = TreeNode(4) root.left.left = TreeNode(2) root.right = TreeNode(10) root.right.left = TreeNode(7) root.right.right = TreeNode(15)
Output
True
- Related Articles
- Program to check whether leaves sequences are same of two leaves or not in python
- Program to check all values in the tree are same or not in Python
- C# program to check whether two sequences are the same or not
- Program to check whether all palindromic substrings are of odd length or not in Python
- Program to check whether parentheses are balanced or not in Python
- Program to check whether we can take all courses or not in Python
- Program to check whether we can unlock all rooms or not in python
- Program to check whether all can get a seat or not in Python
- Program to check whether elements frequencies are even or not in Python
- Program to check whether two sentences are similar or not in Python
- Program to check whether two string arrays are equivalent or not in Python
- Program to check whether every one has at least a friend or not in Python
- Program to check whether each node value except leaves is sum of its children value or not in Python
- Program to check whether domain and range are forming function or not in Python
- Program to check all listed delivery operations are valid or not in Python

Advertisements