Skewed Binary Tree - GeeksforGeeks

22 Jul, 2021
A skewed binary tree is a type of binary tree in which all the nodes have only either one child or no child.
Types of Skewed Binary trees 
There are 2 special types of skewed tree: 
1. Left Skewed Binary Tree: These are those skewed binary trees in which all the nodes are having a left child or no child at all. It is a left side dominated tree. All the right children remain as null.
Below is an example of a left-skewed tree:
C++
Java
Python3
C#
Javascript
#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);} // Driver codeint main(){    /*            1           /          2         /        3    */    Node* root = newNode(1);    root->left = newNode(2);    root->left->left = newNode(3);     return 0;}
// Java implementation of above approachimport java.util.*; class GFG{     // A Tree nodestatic 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);}   // Driver codepublic static void main(String args[]){    /*            1           /          2         /        3    */    Node root = newNode(1);    root.left = newNode(2);    root.left.left = newNode(3);}} // This code is contributed by Arnab Kundu
# Python3 implementation of the above approach # Class that represents an individual# node in a Binary Treeclass Node:    def __init__(self, key):                 self.left = None        self.right = None        self.val = key         # Driver code """         1           /          2         /        3     """root = Node(1)root.left = Node(2)root.left.left = Node(2) # This code is contributed by dhruvsantoshwar
// C# implementation of above approachusing System;  class GFG{              // A Tree node     public 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);    }              // Driver code    public static void Main()    {        /*                1            /            2            /            3        */        Node root = newNode(1);        root.left = newNode(2);        root.left.left = newNode(3);    }} // This code is contributed by AnkitRai01
<script>// Javascript implementation of above approach // A Tree nodeclass Node{    constructor()    {        this.key=0;        this.left=this.right=null;    }} // Utility function to create a new nodefunction newNode(key){    let temp = new Node();    temp.key = key;    temp.left = temp.right = null;         return (temp);} // Driver code /*            1           /          2         /        3    */let root = newNode(1);root.left = newNode(2);root.left.left = newNode(3);  // This code is contributed by avanitrachhadiya2155</script>
2. Right Skewed Binary Tree: These are those skewed binary trees in which all the nodes are having a right child or no child at all. It is a right side dominated tree. All the left children remain as null.
Below is an example of a right-skewed tree:
C++
Java
Python3
C#
Javascript
#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);} // Driver codeint main(){    /*        1         \          2           \            3    */    Node* root = newNode(1);    root->right = newNode(2);    root->right->right = newNode(3);     return 0;}
// Java implementation of above approachimport java.util.*;class GFG{     // A Tree nodestatic 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);}   // Driver codepublic static void main(String args[]){    /*       1        \         2          \           3    */    Node root = newNode(1);    root.right = newNode(2);    root.right.right = newNode(3);}} // This code is contributed by Arnab Kundu
# Python3 implementation of the above approach # A Tree nodeclass Node:         def __init__(self, key):                 self.left = None        self.right = None        self.val = key         # Driver code"""                1         \          2           \            3                 """root = Node(1)root.right = Node(2)root.right.right = Node(3) # This code is contributed by shivanisinghss2110
// C# implementation of above approachusing System;  class GFG{       // A Tree nodepublic class 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);}     // Driver codepublic static void Main(String []args){    /*       1        \         2          \           3    */    Node root = newNode(1);    root.right = newNode(2);    root.right.right = newNode(3);}}  // This code is contributed by PrinciRaj1992
<script> // Javascript implementation of above approach // A Tree nodeclass Node{    constructor()    {        this.key = 0;        this.left = this.right = null;    }} // Utility function to create a new nodefunction newNode(key){    let temp = new Node();    temp.key = key;    temp.left = temp.right = null;         return(temp);} // Driver code /*            1           /          2         /        3    */let root = newNode(1);root.right = newNode(2);root.right.right = newNode(3); // This code is contributed by shivanisinghss2110 </script>
andrew1234
ankthon
princiraj1992
dhruvsantoshwar
avanitrachhadiya2155
shivanisinghss2110
Binary Tree
Data Structures
Tree
Data Structures
Tree
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
SDE SHEET - A Complete Guide for SDE Preparation
DSA Sheet by Love Babbar
Introduction to Algorithms
Introduction to Tree Data Structure
Hash Map in Python
Tree Traversals (Inorder, Preorder and Postorder)
Binary Tree | Set 1 (Introduction)
Level Order Binary Tree Traversal
Inorder Tree Traversal without Recursion
Binary Tree | Set 3 (Types of Binary Tree)