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++
#include <bits/stdc++.h>
using namespace std;
struct Node {
int key;
struct Node *left, *right;
};
Node* newNode( int key)
{
Node* temp = new Node;
temp->key = key;
temp->left = temp->right = NULL;
return (temp);
}
bool isSkewedBT(Node* root)
{
if (root == NULL || (root->left == NULL &&
root->right == NULL))
return true ;
if (root->left && root->right)
return false ;
if (root->left)
return isSkewedBT(root->left);
return isSkewedBT(root->right);
}
int main()
{
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
class Solution
{
static class Node {
int key;
Node left, right;
}
static Node newNode( int key)
{
Node temp = new Node();
temp.key = key;
temp.left = temp.right = null ;
return (temp);
}
static boolean isSkewedBT(Node root)
{
if (root == null || (root.left == null &&
root.right == null ))
return true ;
if (root.left!= null && root.right!= null )
return false ;
if (root.left!= null )
return isSkewedBT(root.left);
return isSkewedBT(root.right);
}
public static void main(String args[])
{
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 );
}
}
|
Python3
class newNode:
def __init__( self , key):
self .data = key
self .left = None
self .right = None
def isSkewedBT(root):
if (root = = None or (root.left = = None and
root.right = = None )):
return 1
if (root.left and root.right):
return 0
if (root.left) :
return isSkewedBT(root.left)
return isSkewedBT(root.right)
if __name__ = = '__main__' :
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))
|
C#
using System;
public class GFG
{
class Node
{
public int key;
public Node left, right;
}
static Node newNode( int key)
{
Node temp = new Node();
temp.key = key;
temp.left = temp.right = null ;
return (temp);
}
static bool isSkewedBT(Node root)
{
if (root == null || (root.left == null &&
root.right == null ))
return true ;
if (root.left!= null && root.right!= null )
return false ;
if (root.left!= null )
return isSkewedBT(root.left);
return isSkewedBT(root.right);
}
public static void Main()
{
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 );
}
}
|
Javascript
<script>
class Node
{
constructor(key) {
this .left = null ;
this .right = null ;
this .data = key;
}
}
function newNode(key)
{
let temp = new Node(key);
return (temp);
}
function isSkewedBT(root)
{
if (root == null || (root.left == null &&
root.right == null ))
return true ;
if (root.left!= null && root.right!= null )
return false ;
if (root.left!= null )
return isSkewedBT(root.left);
return isSkewedBT(root.right);
}
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>" );
</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++
#include <bits/stdc++.h>
using namespace std;
struct Node {
int key;
struct Node *left, *right;
};
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) {
if (root->left == NULL && root->right == NULL)
return true ;
if (root->left != NULL && root->right != NULL)
return false ;
if (root->left != NULL)
root = root->left;
else
root = root->right;
}
return true ;
}
int main()
{
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
class Solution {
static class Node {
int key;
Node left, right;
}
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 ) {
if (root.left == null && root.right == null )
return true ;
if (root.left!= null && root.right!= null )
return false ;
if (root.left != null )
root = root.left;
else
root = root.right;
}
return true ;
}
public static void main(String args[])
{
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 );
}
}
|
Python3
class newNode:
def __init__( self , key):
self .data = key
self .left = None
self .right = None
def isSkewedBT(root):
while (root ! = None ):
if (root.left = = None and root.right = = None ):
return 1
if (root.left ! = None and root.right ! = None ):
return 0
if (root.left ! = None ):
root = root.left
else :
root = root.right
return 1
if __name__ = = '__main__' :
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))
|
C#
using System;
public class GFG {
class Node {
public int key;
public Node left, right;
}
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 ) {
if (root.left == null && root.right == null )
return true ;
if (root.left != null && root.right != null )
return false ;
if (root.left != null )
root = root.left;
else
root = root.right;
}
return true ;
}
public static void Main()
{
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);
}
}
|
Javascript
<script>
class Node
{
constructor(key) {
this .left = null ;
this .right = null ;
this .data = key;
}
}
function newNode(key)
{
let temp = new Node(key);
return (temp);
}
function isSkewedBT(root)
{
while (root != null ) {
if (root.left == null && root.right == null )
return true ;
if (root.left != null && root.right != null )
return false ;
if (root.left != null )
root = root.left;
else
root = root.right;
}
return true ;
}
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>" );
</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.
Please Login to comment...