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 |
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.
Recommended Posts:
- Check whether a binary tree is a full binary tree or not | Iterative Approach
- Check whether a binary tree is a full binary tree or not
- Check if a binary tree is subtree of another binary tree | Set 1
- Check if a binary tree is subtree of another binary tree | Set 2
- Given level order traversal of a Binary Tree, check if the Tree is a Min-Heap
- Check if a given Binary Tree is height balanced like a Red-Black Tree
- Check whether a binary tree is a complete tree or not | Set 2 (Recursive Solution)
- Complexity of different operations in Binary tree, Binary Search Tree and AVL tree
- Minimum swap required to convert binary tree to binary search tree
- Convert a Binary Tree to Threaded binary tree | Set 2 (Efficient)
- Convert a Binary Tree to Threaded binary tree | Set 1 (Using Queue)
- Binary Tree to Binary Search Tree Conversion
- Binary Tree to Binary Search Tree Conversion using STL set
- A program to check if a binary tree is BST or not
- Check whether a given binary tree is perfect or not
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : andrew1234



