Check whether a given binary tree is skewed binary tree or not? - GeeksforGeeks

30 Jun, 2021
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++
Java
Python3
C#
Javascript
// C++ program to Check whether a given// binary tree is skewed binary tree or not? #include <bits/stdc++.h>using namespace std; // A Tree nodestruct Node {    int key;    struct Node *left, *right;}; // Utility function to create a new nodeNode* 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 programint 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 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 nodestatic 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 programpublic 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 program to Check whether a given# binary tree is skewed binary tree or not? # Binary Tree Node""" utility that allocates a newNodewith 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 Codeif __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# program to Check whether a given// binary tree is skewed binary tree or not?using System; public class GFG{     // A Tree nodeclass Node{    public int key;    public Node left, right;} // Utility function to create a new nodestatic 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 codepublic 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*/
<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>
1
1
0
 
Time complexity of this solution is Best case : O(1) when root has two children. Worst case : O(h) when tree is skewed tree. 
andrew1234
Rajput-Ji
SHUBHAMSINGH10
decode2207
Binary Tree
Technical Scripter 2018
Technical Scripter
Tree
Tree
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Tree Traversals (Inorder, Preorder and Postorder)
Binary Tree | Set 1 (Introduction)
Level Order Binary Tree Traversal
Binary Tree | Set 3 (Types of Binary Tree)
Inorder Tree Traversal without Recursion
Write a Program to Find the Maximum Depth or Height of a Tree
Binary Tree | Set 2 (Properties)
Decision Tree
A program to check if a binary tree is BST or not
Construct Tree from given Inorder and Preorder traversals