Check whether a given binary tree is skewed binary tree or not?
Last Updated :
08 Sep, 2022
Given a Binary Tree check whether it is skewed binary tree or not. A skewed tree is a tree where each node has only one child node or none.
Examples:
Input : 5
/
4
\
3
/
2
Output : Yes
Input : 5
/
4
\
3
/ \
2 4
Output : No
The idea is to check if a node has two children. If node has two children return false, else recursively compute whether subtree with one child is skewed tree. If node is leaf node return true.
Below is the implementation of above approach:
C++
// C++ program to Check whether a given
// binary tree is skewed binary tree or not?
#include <bits/stdc++.h>
using namespace std;
// A Tree node
struct Node {
int key;
struct Node *left, *right;
};
// Utility function to create a new node
Node* newNode(int key)
{
Node* temp = new Node;
temp->key = key;
temp->left = temp->right = NULL;
return (temp);
}
bool isSkewedBT(Node* root)
{
// check if node is NULL or is a leaf node
if (root == NULL || (root->left == NULL &&
root->right == NULL))
return true;
// check if node has two children if
// yes, return false
if (root->left && root->right)
return false;
if (root->left)
return isSkewedBT(root->left);
return isSkewedBT(root->right);
}
// Driver program
int main()
{
/* 10
/ \
12 13
/ \
14 15
/ \ / \
21 22 23 24
Let us create Binary Tree shown in above example */
Node* root = newNode(10);
root->left = newNode(12);
root->left->right = newNode(15);
cout << isSkewedBT(root) << endl;
root = newNode(5);
root->right = newNode(4);
root->right->left = newNode(3);
root->right->left->right = newNode(2);
cout << isSkewedBT(root) << endl;
root = newNode(5);
root->left = newNode(4);
root->left->right = newNode(3);
root->left->right->left = newNode(2);
root->left->right->right = newNode(4);
cout << isSkewedBT(root) << endl;
}
Java
// Java program to Check whether a given
// binary tree is skewed binary tree or not?
class Solution
{
// A Tree node
static class Node {
int key;
Node left, right;
}
// Utility function to create a new node
static Node newNode(int key)
{
Node temp = new Node();
temp.key = key;
temp.left = temp.right = null;
return (temp);
}
static boolean isSkewedBT(Node root)
{
// check if node is null or is a leaf node
if (root == null || (root.left == null &&
root.right == null))
return true;
// check if node has two children if
// yes, return false
if (root.left!=null && root.right!=null)
return false;
if (root.left!=null)
return isSkewedBT(root.left);
return isSkewedBT(root.right);
}
// Driver program
public static void main(String args[])
{
/* 10
/ \
12 13
/ \
14 15
/ \ / \
21 22 23 24
Let us create Binary Tree shown in above example */
Node root = newNode(10);
root.left = newNode(12);
root.left.right = newNode(15);
System.out.println( isSkewedBT(root)?1:0 );
root = newNode(5);
root.right = newNode(4);
root.right.left = newNode(3);
root.right.left.right = newNode(2);
System.out.println( isSkewedBT(root)?1:0 );
root = newNode(5);
root.left = newNode(4);
root.left.right = newNode(3);
root.left.right.left = newNode(2);
root.left.right.right = newNode(4);
System.out.println( isSkewedBT(root)?1:0 );
}
}
//contributed by Arnab Kundu
Python3
# Python3 program to Check whether a given
# binary tree is skewed binary tree or not?
# Binary Tree Node
""" utility that allocates a newNode
with the given key """
class newNode:
# Construct to create a newNode
def __init__(self, key):
self.data = key
self.left = None
self.right = None
def isSkewedBT(root):
# check if node is None or is a leaf node
if (root == None or (root.left == None and
root.right == None)):
return 1
# check if node has two children if
# yes, return false
if (root.left and root.right):
return 0
if (root.left) :
return isSkewedBT(root.left)
return isSkewedBT(root.right)
# Driver Code
if __name__ == '__main__':
""" 10
/ \
12 13
/ \
14 15
/ \ / \
21 22 23 24
Let us create Binary Tree shown in above example """
root = newNode(10)
root.left = newNode(12)
root.left.right = newNode(15)
print(isSkewedBT(root))
root = newNode(5)
root.right = newNode(4)
root.right.left = newNode(3)
root.right.left.right = newNode(2)
print(isSkewedBT(root))
root = newNode(5)
root.left = newNode(4)
root.left.right = newNode(3)
root.left.right.left = newNode(2)
root.left.right.right = newNode(4)
print(isSkewedBT(root))
# This code is contributed by
# Shubham Singh(SHUBHAMSINGH10)
C#
// C# program to Check whether a given
// binary tree is skewed binary tree or not?
using System;
public class GFG
{
// A Tree node
class Node
{
public int key;
public Node left, right;
}
// Utility function to create a new node
static Node newNode(int key)
{
Node temp = new Node();
temp.key = key;
temp.left = temp.right = null;
return (temp);
}
static bool isSkewedBT(Node root)
{
// check if node is null or is a leaf node
if (root == null || (root.left == null &&
root.right == null))
return true;
// check if node has two children if
// yes, return false
if (root.left!=null && root.right!=null)
return false;
if (root.left!=null)
return isSkewedBT(root.left);
return isSkewedBT(root.right);
}
// Driver code
public static void Main()
{
/* 10
/ \
12 13
/ \
14 15
/ \ / \
21 22 23 24
Let us create Binary Tree shown in above example */
Node root = newNode(10);
root.left = newNode(12);
root.left.right = newNode(15);
Console.WriteLine( isSkewedBT(root)?1:0 );
root = newNode(5);
root.right = newNode(4);
root.right.left = newNode(3);
root.right.left.right = newNode(2);
Console.WriteLine( isSkewedBT(root)?1:0 );
root = newNode(5);
root.left = newNode(4);
root.left.right = newNode(3);
root.left.right.left = newNode(2);
root.left.right.right = newNode(4);
Console.WriteLine( isSkewedBT(root)?1:0 );
}
}
/* This code is contributed by Rajput-Ji*/
JavaScript
<script>
// Javascript program to Check whether a given
// binary tree is skewed binary tree or not?
// Binary tree node
class Node
{
constructor(key) {
this.left = null;
this.right = null;
this.data = key;
}
}
// Utility function to create a new node
function newNode(key)
{
let temp = new Node(key);
return (temp);
}
function isSkewedBT(root)
{
// check if node is null or is a leaf node
if (root == null || (root.left == null &&
root.right == null))
return true;
// check if node has two children if
// yes, return false
if (root.left!=null && root.right!=null)
return false;
if (root.left!=null)
return isSkewedBT(root.left);
return isSkewedBT(root.right);
}
/* 10
/ \
12 13
/ \
14 15
/ \ / \
21 22 23 24
Let us create Binary Tree shown in above example */
let root = newNode(10);
root.left = newNode(12);
root.left.right = newNode(15);
document.write(isSkewedBT(root)?1 + "</br>":0 + "</br>");
root = newNode(5);
root.right = newNode(4);
root.right.left = newNode(3);
root.right.left.right = newNode(2);
document.write(isSkewedBT(root)?1 + "</br>":0 + "</br>");
root = newNode(5);
root.left = newNode(4);
root.left.right = newNode(3);
root.left.right.left = newNode(2);
root.left.right.right = newNode(4);
document.write(isSkewedBT(root)?1 + "</br>":0 + "</br>");
// This code is contributed by decode2207.
</script>
Complexity Analysis:
- Time complexity:
- Best case : O(1) when root has two children.
- Worst case : O(h) when tree is skewed tree.
- Auxiliary Space: O(h)
- Here h is the height of the tree
Another approach:(iterative method)
We can also check if a tree is skewed or not by iterative method using above approach.
Implementation:
C++
// C++ program to Check whether a given
// binary tree is skewed binary tree or not using iterative
// method
#include <bits/stdc++.h>
using namespace std;
// A Tree node
struct Node {
int key;
struct Node *left, *right;
};
// Utility function to create a new node
Node* newNode(int key)
{
Node* temp = new Node;
temp->key = key;
temp->left = temp->right = NULL;
return (temp);
}
bool isSkewedBT(Node* root)
{
while (root != NULL) {
// check if the node is a leaf node
if (root->left == NULL && root->right == NULL)
return true;
// check if node has two children if
// yes, return false
if (root->left != NULL && root->right != NULL)
return false;
// move towards left if it has a left child
if (root->left != NULL)
root = root->left;
// or move towards right if it has a right child
else
root = root->right;
}
return true;
}
// Driver program
int main()
{
/* 10
/ \
12 13
/ \
14 15
/ \ / \
21 22 23 24
Let us create Binary Tree shown in above example */
Node* root = newNode(10);
root->left = newNode(12);
root->left->right = newNode(15);
cout << isSkewedBT(root) << endl;
root = newNode(5);
root->right = newNode(4);
root->right->left = newNode(3);
root->right->left->right = newNode(2);
cout << isSkewedBT(root) << endl;
root = newNode(5);
root->left = newNode(4);
root->left->right = newNode(3);
root->left->right->left = newNode(2);
root->left->right->right = newNode(4);
cout << isSkewedBT(root) << endl;
}
// This code is contributed by Abhijeet Kumar(abhijeet19403)
Java
// Java program to Check whether a given
// binary tree is skewed binary tree or not using iterative method
class Solution {
// A Tree node
static class Node {
int key;
Node left, right;
}
// Utility function to create a new node
static Node newNode(int key)
{
Node temp = new Node();
temp.key = key;
temp.left = temp.right = null;
return (temp);
}
static boolean isSkewedBT(Node root)
{
while (root != null) {
// check if the node is a leaf node
if (root.left == null && root.right == null)
return true;
// check if node has two children if
// yes, return false
if (root.left!=null && root.right!=null)
return false;
// move towards left if it has a left child
if (root.left != null)
root = root.left;
// or move towards right if it has a right child
else
root = root.right;
}
return true;
}
// Driver program
public static void main(String args[])
{
/* 10
/ \
12 13
/ \
14 15
/ \ / \
21 22 23 24
Let us create Binary Tree shown in above example */
Node root = newNode(10);
root.left = newNode(12);
root.left.right = newNode(15);
System.out.println(isSkewedBT(root) ? 1 : 0);
root = newNode(5);
root.right = newNode(4);
root.right.left = newNode(3);
root.right.left.right = newNode(2);
System.out.println(isSkewedBT(root) ? 1 : 0);
root = newNode(5);
root.left = newNode(4);
root.left.right = newNode(3);
root.left.right.left = newNode(2);
root.left.right.right = newNode(4);
System.out.println(isSkewedBT(root) ? 1 : 0);
}
}
// This code is contributed by Abhijeet Kumar(abhijeet19403)
Python3
# Python3 program to Check whether a given
# binary tree is skewed binary tree or not using iterative method
# Binary Tree Node
""" utility that allocates a newNode
with the given key """
class newNode:
# Construct to create a newNode
def __init__(self, key):
self.data = key
self.left = None
self.right = None
def isSkewedBT(root):
while (root != None):
# check if the node is a leaf node
if (root.left == None and root.right == None):
return 1
# check if node has two children if
# yes, return false
if (root.left != None and root.right != None):
return 0
# move towards left if it has a left child
if (root.left != None):
root = root.left
# or move towards right if it has a right child
else:
root = root.right
return 1
# Driver Code
if __name__ == '__main__':
""" 10
/ \
12 13
/ \
14 15
/ \ / \
21 22 23 24
Let us create Binary Tree shown in above example """
root = newNode(10)
root.left = newNode(12)
root.left.right = newNode(15)
print(isSkewedBT(root))
root = newNode(5)
root.right = newNode(4)
root.right.left = newNode(3)
root.right.left.right = newNode(2)
print(isSkewedBT(root))
root = newNode(5)
root.left = newNode(4)
root.left.right = newNode(3)
root.left.right.left = newNode(2)
root.left.right.right = newNode(4)
print(isSkewedBT(root))
# This code is contributed by
# Abhijeet Kumar(abhijeet19403)
C#
// C# program to Check whether a given
// binary tree is skewed binary tree or not using iterative method
using System;
public class GFG {
// A Tree node
class Node {
public int key;
public Node left, right;
}
// Utility function to create a new node
static Node newNode(int key)
{
Node temp = new Node();
temp.key = key;
temp.left = temp.right = null;
return (temp);
}
static bool isSkewedBT(Node root)
{
while (root != null) {
// check if the node is a leaf node
if (root.left == null && root.right == null)
return true;
// check if node has two children if
// yes, return false
if (root.left != null && root.right != null)
return false;
// move towards left if it has a left child
if (root.left != null)
root = root.left;
// or move towards right if it has a right child
else
root = root.right;
}
return true;
}
// Driver code
public static void Main()
{
/* 10
/ \
12 13
/ \
14 15
/ \ / \
21 22 23 24
Let us create Binary Tree shown in above example */
Node root = newNode(10);
root.left = newNode(12);
root.left.right = newNode(15);
Console.WriteLine(isSkewedBT(root) ? 1 : 0);
root = newNode(5);
root.right = newNode(4);
root.right.left = newNode(3);
root.right.left.right = newNode(2);
Console.WriteLine(isSkewedBT(root) ? 1 : 0);
root = newNode(5);
root.left = newNode(4);
root.left.right = newNode(3);
root.left.right.left = newNode(2);
root.left.right.right = newNode(4);
Console.WriteLine(isSkewedBT(root) ? 1 : 0);
}
}
/* This code is contributed by Abhijeet
* Kumar(abhijeet19403)*/
JavaScript
<script>
// Javascript program to Check whether a given
// binary tree is skewed binary tree or not?
// Binary tree node
class Node
{
constructor(key) {
this.left = null;
this.right = null;
this.data = key;
}
}
// Utility function to create a new node
function newNode(key)
{
let temp = new Node(key);
return (temp);
}
function isSkewedBT(root)
{
while (root != null) {
// check if the node is a leaf node
if (root.left == null && root.right == null)
return true;
// check if node has two children if
// yes, return false
if (root.left != null && root.right != null)
return false;
// move towards left if it has a left child
if (root.left != null)
root = root.left;
// or move towards right if it has a right child
else
root = root.right;
}
return true;
}
/* 10
/ \
12 13
/ \
14 15
/ \ / \
21 22 23 24
Let us create Binary Tree shown in above example */
let root = newNode(10);
root.left = newNode(12);
root.left.right = newNode(15);
document.write(isSkewedBT(root)?1 + "</br>":0 + "</br>");
root = newNode(5);
root.right = newNode(4);
root.right.left = newNode(3);
root.right.left.right = newNode(2);
document.write(isSkewedBT(root)?1 + "</br>":0 + "</br>");
root = newNode(5);
root.left = newNode(4);
root.left.right = newNode(3);
root.left.right.left = newNode(2);
root.left.right.right = newNode(4);
document.write(isSkewedBT(root)?1 + "</br>":0 + "</br>");
// This code is contributed by Abhijeet Kumar(abhijeet19403)
</script>
Complexity Analysis:
- Time Complexity: O(n)
- As in the worst case we have to visit every node.
- Auxiliary Space: O(1)
- As constant extra space is used.
This approach was contributed by Ahijeet Kumar.
Similar Reads
Check whether a binary tree is a full binary tree or not
A full binary tree is defined as a binary tree in which all nodes have either zero or two child nodes. Conversely, there is no node in a full binary tree, which has one child node. More information about full binary trees can be found here. For Example : Recommended PracticeFull binary treeTry It! T
14 min read
Check whether a given binary tree is perfect or not
Given a Binary Tree, the task is to check whether the given Binary Tree is a perfect Binary Tree or not.Note:A Binary tree is a Perfect Binary Tree in which all internal nodes have two children and all leaves are at the same level.A Perfect Binary Tree of height h has 2h â 1 nodes.Examples: Input:Ou
13 min read
Check if the given n-ary tree is a binary tree
Given an n-ary tree consisting of n nodes, the task is to check whether the given tree is binary or not.Note: An n-ary tree is a tree where each node can have zero or more children nodes. Unlike a binary tree, which has at most two children per node (left and right), the n-ary tree allows for multip
6 min read
Check whether a binary tree is a full binary tree or not | Iterative Approach
Given a binary tree containing n nodes. The problem is to check whether the given binary tree is a full binary tree or not. A full binary tree is defined as a binary tree in which all nodes have either zero or two child nodes. Conversely, there is no node in a full binary tree, which has only one ch
8 min read
Check if a Binary Tree is an Even-Odd Tree or not
Given a Binary Tree, the task is to check if the binary tree is an Even-Odd binary tree or not. A Binary Tree is called an Even-Odd Tree when all the nodes which are at even levels have even values (assuming root to be at level 0) and all the nodes which are at odd levels have odd values. Examples:
15+ min read
Check if a Binary tree is Subtree of another Binary tree | Set 3
Given two binary trees, check if the first tree is a subtree of the second one. A subtree of a tree T(root1) is a tree S(root2) consisting of a node in T and all of its descendants in T. The subtree corresponding to the root node is the entire tree and the subtree corresponding to any other node is
12 min read
Check if a Binary Tree is subtree of another binary tree | Set 1
Given two binary trees, check if the first tree is a subtree of the second one. A subtree of a tree T(root1) is a tree S(root2) consisting of a node in T and all of its descendants in T. The subtree corresponding to the root node is the entire tree and the subtree corresponding to any other node is
9 min read
Check whether a binary tree is a complete tree or not | Set 2 (Recursive Solution)
A complete binary tree is a binary tree whose all levels except the last level are completely filled and all the leaves in the last level are all to the left side. More information about complete binary trees can be found here. Example:Below tree is a Complete Binary Tree (All nodes till the second
10 min read
Check if a binary tree is subtree of another binary tree | Set 2
Given two binary trees, check if the first tree is a subtree of the second one. A subtree of a tree T is a tree S consisting of a node in T and all of its descendants in T. The subtree corresponding to the root node is the entire tree; the subtree corresponding to any other node is called a proper s
15+ min read
Check if a given Binary Tree is Sum Tree
Given a binary tree, the task is to check if it is a Sum Tree. A Sum Tree is a Binary Tree where the value of a node is equal to the sum of the nodes present in its left subtree and right subtree. An empty tree is Sum Tree and the sum of an empty tree can be considered as 0. A leaf node is also cons
15+ min read