Check whether given binary is skewed binary tree

Here given code implementation process.

// C program for 
// Check whether given binary is skewed binary tree 
#include <stdio.h>

#include <stdlib.h>

// Tree Node
struct TreeNode
{
	int data;
	struct TreeNode *left;
	struct TreeNode *right;
};
// Binary Tree
struct BinaryTree
{
	struct TreeNode *root;
};
// Create new tree
struct BinaryTree *new_tree()
{
	// Create dynamic node
	struct BinaryTree *tree = (struct BinaryTree *) malloc(sizeof(struct BinaryTree));
	if (tree != NULL)
	{
		tree->root = NULL;
	}
	else
	{
		printf("Memory Overflow to Create tree Tree\n");
	}
	//return new tree
	return tree;
}
// This is creates and returns the new binary tree node
struct TreeNode *get_node(int data)
{
	// Create dynamic node
	struct TreeNode *node = (struct TreeNode *) malloc(sizeof(struct TreeNode));
	if (node != NULL)
	{
		//Set data and pointer values
		node->data = data;
		node->left = NULL;
		node->right = NULL;
	}
	else
	{
		//This is indicates, segmentation fault or memory overflow problem
		printf("Memory Overflow\n");
	}
	//return new node
	return node;
}
// Recursive inorder traversal of binary tree
void inorder(struct TreeNode *node)
{
	if (node != NULL)
	{
		inorder(node->left);
		printf("  %d", node->data);
		inorder(node->right);
	}
}
// Determine that tree is skewed tree or not
int skewedTree(struct TreeNode *node)
{
	if (node == NULL)
	{
		return 1;
	}
	if (node->left != NULL && node->right != NULL)
	{
		// When node contains both child
		return 0;
	}
	// Recursively check skewed tree
	return skewedTree(node->left) && skewedTree(node->right);
}
void isSkewedBinaryTree(struct TreeNode *root)
{
	if (root == NULL)
	{
		// When tree is empty
		printf("\n Empty Tree");
	}
	else
	{
		// Display given tree node
		printf("\n Inorder Traversal \n");
		inorder(root);
		if (skewedTree(root) == 1)
		{
			printf("\n Is Skewed Binary Tree \n");
		}
		else
		{
			printf("\n Is Not Skewed Binary Tree \n");
		}
	}
}
int main()
{
	struct BinaryTree *tree1 = new_tree();
	struct BinaryTree *tree2 = new_tree();
	/*
	Create First Binary Tree

	    4
	   /   
	  8    
	   \  
	    6 
	   /   
	  9  
	   \
	    7
	     \
	      1  
	*/
	// Add tree node
	tree1->root = get_node(4);
	tree1->root->left = get_node(8);
	tree1->root->left->right = get_node(6);
	tree1->root->left->right->left = get_node(9);
	tree1->root->left->right->left->right = get_node(7);
	tree1->root->left->right->left->right->right = get_node(1);
	/*
	Create Second Binary Tree 

	    4
	   /   
	  8    
	   \  
	    6 
	   / \ 
	  9   3
	   \
	    7
	     \
	      1  
	*/
	// Add tree node
	tree2->root = get_node(4);
	tree2->root->left = get_node(8);
	tree2->root->left->right = get_node(6);
	tree2->root->left->right->right = get_node(3);
	tree2->root->left->right->left = get_node(9);
	tree2->root->left->right->left->right = get_node(7);
	tree2->root->left->right->left->right->right = get_node(1);
	// Test
	isSkewedBinaryTree(tree1->root);
	isSkewedBinaryTree(tree2->root);
	return 0;
}

input

 Inorder Traversal
  8  9  7  1  6  4
 Is Skewed Binary Tree

 Inorder Traversal
  8  9  7  1  6  3  4
 Is Not Skewed Binary Tree
/*
    Java Program
    Check whether given binary is skewed binary tree 
*/
//Binary Tree node
class TreeNode
{
	public int data;
	public TreeNode left;
	public TreeNode right;
	public TreeNode(int data)
	{
		// Set node value
		this.data = data;
		this.left = null;
		this.right = null;
	}
}
public class BinaryTree
{
	public TreeNode root;
	public BinaryTree()
	{
		// Set initial tree root to null
		this.root = null;
	}
	// Recursive inorder traversal of binary tree
	public void inorder(TreeNode node)
	{
		if (node != null)
		{
			inorder(node.left);
			System.out.print("  " + node.data);
			inorder(node.right);
		}
	}
	// Determine that tree is skewed tree or not
	public boolean skewedTree(TreeNode node)
	{
		if (node == null)
		{
			return true;
		}
		if (node.left != null && node.right != null)
		{
			// When node contains both child
			return false;
		}
		// Recursively check skewed tree
		return skewedTree(node.left) && skewedTree(node.right);
	}
	public void isSkewedBinaryTree()
	{
		if (this.root == null)
		{
			// When tree is empty
			System.out.print("\n Empty Tree");
		}
		else
		{
			// Display given tree node
			System.out.println("\n Inorder Traversal ");
			inorder(this.root);
			if (skewedTree(root) == true)
			{
				System.out.println("\n Is Skewed Binary Tree ");
			}
			else
			{
				System.out.println("\n Is Not Skewed Binary Tree ");
			}
		}
	}
	public static void main(String[] args)
	{
		// Create new binary trees 
		BinaryTree tree1 = new BinaryTree();
		BinaryTree tree2 = new BinaryTree();
		/*
		    Create First Binary Tree

		    4
		   /   
		  8    
		   \  
		    6 
		   /   
		  9  
		   \
		    7
		     \
		      1  
		*/
		// Add tree node
		tree1.root = new TreeNode(4);
		tree1.root.left = new TreeNode(8);
		tree1.root.left.right = new TreeNode(6);
		tree1.root.left.right.left = new TreeNode(9);
		tree1.root.left.right.left.right = new TreeNode(7);
		tree1.root.left.right.left.right.right = new TreeNode(1);
		/*
		    Create Second Binary Tree 

		    4
		   /   
		  8    
		   \  
		    6 
		   / \ 
		  9   3
		   \
		    7
		     \
		      1  
		 */
		// Add tree node
		tree2.root = new TreeNode(4);
		tree2.root.left = new TreeNode(8);
		tree2.root.left.right = new TreeNode(6);
		tree2.root.left.right.right = new TreeNode(3);
		tree2.root.left.right.left = new TreeNode(9);
		tree2.root.left.right.left.right = new TreeNode(7);
		tree2.root.left.right.left.right.right = new TreeNode(1);
		// Test
		tree1.isSkewedBinaryTree();
		tree2.isSkewedBinaryTree();
	}
}

input

 Inorder Traversal
  8  9  7  1  6  4
 Is Skewed Binary Tree

 Inorder Traversal
  8  9  7  1  6  3  4
 Is Not Skewed Binary Tree
// Include header file
#include <iostream>
using namespace std;

/*
    C++ Program
    Check whether given binary is skewed binary tree 
*/

//Binary Tree node
class TreeNode
{
	public: int data;
	TreeNode *left;
	TreeNode *right;
	TreeNode(int data)
	{
		// Set node value
		this->data = data;
		this->left = NULL;
		this->right = NULL;
	}
};
class BinaryTree
{
	public: TreeNode *root;
	BinaryTree()
	{
		this->root = NULL;
	}
	// Recursive inorder traversal of binary tree
	void inorder(TreeNode *node)
	{
		if (node != NULL)
		{
			this->inorder(node->left);
			cout << "  " << node->data;
			this->inorder(node->right);
		}
	}
	// Determine that tree is skewed tree or not
	bool skewedTree(TreeNode *node)
	{
		if (node == NULL)
		{
			return true;
		}
		if (node->left != NULL && node->right != NULL)
		{
			// When node contains both child
			return false;
		}
		// Recursively check skewed tree
		return this->skewedTree(node->left) && this->skewedTree(node->right);
	}
	void isSkewedBinaryTree()
	{
		if (this->root == NULL)
		{
			// When tree is empty
			cout << "\n Empty Tree";
		}
		else
		{
			// Display given tree node
			cout << "\n Inorder Traversal " << endl;
			this->inorder(this->root);
			if (this->skewedTree(this->root) == true)
			{
				cout << "\n Is Skewed Binary Tree " << endl;
			}
			else
			{
				cout << "\n Is Not Skewed Binary Tree " << endl;
			}
		}
	}
};
int main()
{
	// Create new binary trees
	BinaryTree *tree1 = new BinaryTree();
	BinaryTree *tree2 = new BinaryTree();
	/*
	    Create First Binary Tree
	    4
	   /   
	  8    
	   \  
	    6 
	   /   
	  9  
	   \
	    7
	     \
	      1  
	*/
	// Add tree node
	tree1->root = new TreeNode(4);
	tree1->root->left = new TreeNode(8);
	tree1->root->left->right = new TreeNode(6);
	tree1->root->left->right->left = new TreeNode(9);
	tree1->root->left->right->left->right = new TreeNode(7);
	tree1->root->left->right->left->right->right = new TreeNode(1);
	/*
	    Create Second Binary Tree 
	    4
	   /   
	  8    
	   \  
	    6 
	   / \ 
	  9   3
	   \
	    7
	     \
	      1  
	*/
	// Add tree node
	tree2->root = new TreeNode(4);
	tree2->root->left = new TreeNode(8);
	tree2->root->left->right = new TreeNode(6);
	tree2->root->left->right->right = new TreeNode(3);
	tree2->root->left->right->left = new TreeNode(9);
	tree2->root->left->right->left->right = new TreeNode(7);
	tree2->root->left->right->left->right->right = new TreeNode(1);
	// Test
	tree1->isSkewedBinaryTree();
	tree2->isSkewedBinaryTree();
	return 0;
}

input

 Inorder Traversal
  8  9  7  1  6  4
 Is Skewed Binary Tree

 Inorder Traversal
  8  9  7  1  6  3  4
 Is Not Skewed Binary Tree
// Include namespace system
using System;
/*
    Csharp Program
    Check whether given binary is skewed binary tree 
*/

//Binary Tree node
public class TreeNode
{
	public int data;
	public TreeNode left;
	public TreeNode right;
	public TreeNode(int data)
	{
		// Set node value
		this.data = data;
		this.left = null;
		this.right = null;
	}
}
public class BinaryTree
{
	public TreeNode root;
	public BinaryTree()
	{
		// Set initial tree root to null
		this.root = null;
	}
	// Recursive inorder traversal of binary tree
	public void inorder(TreeNode node)
	{
		if (node != null)
		{
			this.inorder(node.left);
			Console.Write("  " + node.data);
			this.inorder(node.right);
		}
	}
	// Determine that tree is skewed tree or not
	public Boolean skewedTree(TreeNode node)
	{
		if (node == null)
		{
			return true;
		}
		if (node.left != null && node.right != null)
		{
			// When node contains both child
			return false;
		}
		// Recursively check skewed tree
		return this.skewedTree(node.left) && this.skewedTree(node.right);
	}
	public void isSkewedBinaryTree()
	{
		if (this.root == null)
		{
			// When tree is empty
			Console.Write("\n Empty Tree");
		}
		else
		{
			// Display given tree node
			Console.WriteLine("\n Inorder Traversal ");
			this.inorder(this.root);
			if (this.skewedTree(this.root) == true)
			{
				Console.WriteLine("\n Is Skewed Binary Tree ");
			}
			else
			{
				Console.WriteLine("\n Is Not Skewed Binary Tree ");
			}
		}
	}
	public static void Main(String[] args)
	{
		// Create new binary trees
		BinaryTree tree1 = new BinaryTree();
		BinaryTree tree2 = new BinaryTree();
		/*
		    Create First Binary Tree
		    4
		   /   
		  8    
		   \  
		    6 
		   /   
		  9  
		   \
		    7
		     \
		      1  
		*/
		// Add tree node
		tree1.root = new TreeNode(4);
		tree1.root.left = new TreeNode(8);
		tree1.root.left.right = new TreeNode(6);
		tree1.root.left.right.left = new TreeNode(9);
		tree1.root.left.right.left.right = new TreeNode(7);
		tree1.root.left.right.left.right.right = new TreeNode(1);
		/*
		    Create Second Binary Tree 
		    4
		   /   
		  8    
		   \  
		    6 
		   / \ 
		  9   3
		   \
		    7
		     \
		      1  
		*/
		// Add tree node
		tree2.root = new TreeNode(4);
		tree2.root.left = new TreeNode(8);
		tree2.root.left.right = new TreeNode(6);
		tree2.root.left.right.right = new TreeNode(3);
		tree2.root.left.right.left = new TreeNode(9);
		tree2.root.left.right.left.right = new TreeNode(7);
		tree2.root.left.right.left.right.right = new TreeNode(1);
		// Test
		tree1.isSkewedBinaryTree();
		tree2.isSkewedBinaryTree();
	}
}

input

 Inorder Traversal
  8  9  7  1  6  4
 Is Skewed Binary Tree

 Inorder Traversal
  8  9  7  1  6  3  4
 Is Not Skewed Binary Tree
<?php
/*
    Php Program
    Check whether given binary is skewed binary tree 
*/

//Binary Tree node
class TreeNode
{
	public $data;
	public $left;
	public $right;
	public	function __construct($data)
	{
		// Set node value
		$this->data = $data;
		$this->left = NULL;
		$this->right = NULL;
	}
}
class BinaryTree
{
	public $root;
	public	function __construct()
	{
		$this->root = NULL;
	}
	// Recursive inorder traversal of binary tree
	public	function inorder($node)
	{
		if ($node != NULL)
		{
			$this->inorder($node->left);
			echo("  ".$node->data);
			$this->inorder($node->right);
		}
	}
	// Determine that tree is skewed tree or not
	public	function skewedTree($node)
	{
		if ($node == NULL)
		{
			return true;
		}
		if ($node->left != NULL && $node->right != NULL)
		{
			// When node contains both child
			return false;
		}
		// Recursively check skewed tree
		return $this->skewedTree($node->left) && 
          $this->skewedTree($node->right);
	}
	public	function isSkewedBinaryTree()
	{
		if ($this->root == NULL)
		{
			// When tree is empty
			echo("\n Empty Tree");
		}
		else
		{
			// Display given tree node
			echo("\n Inorder Traversal ".
				"\n");
			$this->inorder($this->root);
			if ($this->skewedTree($this->root) == true)
			{
				echo("\n Is Skewed Binary Tree ".
					"\n");
			}
			else
			{
				echo("\n Is Not Skewed Binary Tree ".
					"\n");
			}
		}
	}
}

function main()
{
	// Create new binary trees
	$tree1 = new BinaryTree();
	$tree2 = new BinaryTree();
	/*
	    Create First Binary Tree
	    4
	   /   
	  8    
	   \  
	    6 
	   /   
	  9  
	   \
	    7
	     \
	      1  
	*/
	// Add tree node
	$tree1->root = new TreeNode(4);
	$tree1->root->left = new TreeNode(8);
	$tree1->root->left->right = new TreeNode(6);
	$tree1->root->left->right->left = new TreeNode(9);
	$tree1->root->left->right->left->right = new TreeNode(7);
	$tree1->root->left->right->left->right->right = new TreeNode(1);
	/*
	    Create Second Binary Tree 
	    4
	   /   
	  8    
	   \  
	    6 
	   / \ 
	  9   3
	   \
	    7
	     \
	      1  
	*/
	// Add tree node
	$tree2->root = new TreeNode(4);
	$tree2->root->left = new TreeNode(8);
	$tree2->root->left->right = new TreeNode(6);
	$tree2->root->left->right->right = new TreeNode(3);
	$tree2->root->left->right->left = new TreeNode(9);
	$tree2->root->left->right->left->right = new TreeNode(7);
	$tree2->root->left->right->left->right->right = new TreeNode(1);
	// Test
	$tree1->isSkewedBinaryTree();
	$tree2->isSkewedBinaryTree();
}
main();

input

 Inorder Traversal
  8  9  7  1  6  4
 Is Skewed Binary Tree

 Inorder Traversal
  8  9  7  1  6  3  4
 Is Not Skewed Binary Tree
/*
    Node JS Program
    Check whether given binary is skewed binary tree 
*/
// Binary Tree node
class TreeNode
{
	constructor(data)
	{
		// Set node value
		this.data = data;
		this.left = null;
		this.right = null;
	}
}
class BinaryTree
{
	constructor()
	{
		this.root = null;
	}
	// Recursive inorder traversal of binary tree
	inorder(node)
	{
		if (node != null)
		{
			this.inorder(node.left);
			process.stdout.write("  " + node.data);
			this.inorder(node.right);
		}
	}
	// Determine that tree is skewed tree or not
	skewedTree(node)
	{
		if (node == null)
		{
			return true;
		}
		if (node.left != null && node.right != null)
		{
			// When node contains both child
			return false;
		}
		// Recursively check skewed tree
		return this.skewedTree(node.left) && 
          	   this.skewedTree(node.right);
	}
	isSkewedBinaryTree()
	{
		if (this.root == null)
		{
			// When tree is empty
			process.stdout.write("\n Empty Tree");
		}
		else
		{
			// Display given tree node
			console.log("\n Inorder Traversal ");
			this.inorder(this.root);
			if (this.skewedTree(this.root) == true)
			{
				console.log("\n Is Skewed Binary Tree ");
			}
			else
			{
				console.log("\n Is Not Skewed Binary Tree ");
			}
		}
	}
}

function main()
{
	// Create new binary trees
	var tree1 = new BinaryTree();
	var tree2 = new BinaryTree();
	/*
	    Create First Binary Tree
	    4
	   /   
	  8    
	   \  
	    6 
	   /   
	  9  
	   \
	    7
	     \
	      1  
	*/
	// Add tree node
	tree1.root = new TreeNode(4);
	tree1.root.left = new TreeNode(8);
	tree1.root.left.right = new TreeNode(6);
	tree1.root.left.right.left = new TreeNode(9);
	tree1.root.left.right.left.right = new TreeNode(7);
	tree1.root.left.right.left.right.right = new TreeNode(1);
	/*
	    Create Second Binary Tree 
	    4
	   /   
	  8    
	   \  
	    6 
	   / \ 
	  9   3
	   \
	    7
	     \
	      1  
	*/
	// Add tree node
	tree2.root = new TreeNode(4);
	tree2.root.left = new TreeNode(8);
	tree2.root.left.right = new TreeNode(6);
	tree2.root.left.right.right = new TreeNode(3);
	tree2.root.left.right.left = new TreeNode(9);
	tree2.root.left.right.left.right = new TreeNode(7);
	tree2.root.left.right.left.right.right = new TreeNode(1);
	// Test
	tree1.isSkewedBinaryTree();
	tree2.isSkewedBinaryTree();
}
main();

input

 Inorder Traversal
  8  9  7  1  6  4
 Is Skewed Binary Tree

 Inorder Traversal
  8  9  7  1  6  3  4
 Is Not Skewed Binary Tree
#    Python 3 Program
#    Check whether given binary is skewed binary tree 

# Binary Tree node
class TreeNode :
	def __init__(self, data) :
		#  Set node value
		self.data = data
		self.left = None
		self.right = None
	

class BinaryTree :
	def __init__(self) :
		self.root = None
	
	#  Recursive inorder traversal of binary tree
	def inorder(self, node) :
		if (node != None) :
			self.inorder(node.left)
			print("  ", node.data, end = "")
			self.inorder(node.right)
		
	
	#  Determine that tree is skewed tree or not
	def skewedTree(self, node) :
		if (node == None) :
			return True
		
		if (node.left != None and node.right != None) :
			#  When node contains both child
			return False
		
		#  Recursively check skewed tree
		return self.skewedTree(node.left) and self.skewedTree(node.right)
	
	def isSkewedBinaryTree(self) :
		if (self.root == None) :
			#  When tree is empty
			print("\n Empty Tree", end = "")
		else :
			#  Display given tree node
			print("\n Inorder Traversal ")
			self.inorder(self.root)
			if (self.skewedTree(self.root) == True) :
				print("\n Is Skewed Binary Tree ")
			else :
				print("\n Is Not Skewed Binary Tree ")
			
		
	

def main() :
	tree1 = BinaryTree()
	tree2 = BinaryTree()
	#    Create First Binary Tree
	#    4
	#   /   
	#  8    
	#   \  
	#    6 
	#   /   
	#  9  
	#   \
	#    7
	#     \
	#      1  
	#  Add tree node
	tree1.root = TreeNode(4)
	tree1.root.left = TreeNode(8)
	tree1.root.left.right = TreeNode(6)
	tree1.root.left.right.left = TreeNode(9)
	tree1.root.left.right.left.right = TreeNode(7)
	tree1.root.left.right.left.right.right = TreeNode(1)
	#    Create Second Binary Tree 
	#    4
	#   /   
	#  8    
	#   \  
	#    6 
	#   / \ 
	#  9   3
	#   \
	#    7
	#     \
	#      1  
	#  Add tree node
	tree2.root = TreeNode(4)
	tree2.root.left = TreeNode(8)
	tree2.root.left.right = TreeNode(6)
	tree2.root.left.right.right = TreeNode(3)
	tree2.root.left.right.left = TreeNode(9)
	tree2.root.left.right.left.right = TreeNode(7)
	tree2.root.left.right.left.right.right = TreeNode(1)
	#  Test
	tree1.isSkewedBinaryTree()
	tree2.isSkewedBinaryTree()

if __name__ == "__main__": main()

input

 Inorder Traversal
   8   9   7   1   6   4
 Is Skewed Binary Tree

 Inorder Traversal
   8   9   7   1   6   3   4
 Is Not Skewed Binary Tree
#    Ruby Program
#    Check whether given binary is skewed binary tree 

# Binary Tree node
class TreeNode 
	# Define the accessor and reader of class TreeNode
	attr_reader :data, :left, :right
	attr_accessor :data, :left, :right
	def initialize(data) 
		#  Set node value
		self.data = data
		self.left = nil
		self.right = nil
	end

end

class BinaryTree 
	# Define the accessor and reader of class BinaryTree
	attr_reader :root
	attr_accessor :root
	def initialize() 
		self.root = nil
	end

	#  Recursive inorder traversal of binary tree
	def inorder(node) 
		if (node != nil) 
			self.inorder(node.left)
			print("  ", node.data)
			self.inorder(node.right)
		end

	end

	#  Determine that tree is skewed tree or not
	def skewedTree(node) 
		if (node == nil) 
			return true
		end

		if (node.left != nil && node.right != nil) 
			#  When node contains both child
			return false
		end

		#  Recursively check skewed tree
		return self.skewedTree(node.left) && self.skewedTree(node.right)
	end

	def isSkewedBinaryTree() 
		if (self.root == nil) 
			#  When tree is empty
			print("\n Empty Tree")
		else 
			#  Display given tree node
			print("\n Inorder Traversal ", "\n")
			self.inorder(self.root)
			if (self.skewedTree(self.root) == true) 
				print("\n Is Skewed Binary Tree ", "\n")
			else 
				print("\n Is Not Skewed Binary Tree ", "\n")
			end

		end

	end

end

def main() 
	#  Create new binary trees
	tree1 = BinaryTree.new()
	tree2 = BinaryTree.new()
	#    Create First Binary Tree
	#    4
	#   /   
	#  8    
	#   \  
	#    6 
	#   /   
	#  9  
	#   \
	#    7
	#     \
	#      1  
	#  Add tree node
	tree1.root = TreeNode.new(4)
	tree1.root.left = TreeNode.new(8)
	tree1.root.left.right = TreeNode.new(6)
	tree1.root.left.right.left = TreeNode.new(9)
	tree1.root.left.right.left.right = TreeNode.new(7)
	tree1.root.left.right.left.right.right = TreeNode.new(1)
	#    Create Second Binary Tree 
	#    4
	#   /   
	#  8    
	#   \  
	#    6 
	#   / \ 
	#  9   3
	#   \
	#    7
	#     \
	#      1  
	#  Add tree node
	tree2.root = TreeNode.new(4)
	tree2.root.left = TreeNode.new(8)
	tree2.root.left.right = TreeNode.new(6)
	tree2.root.left.right.right = TreeNode.new(3)
	tree2.root.left.right.left = TreeNode.new(9)
	tree2.root.left.right.left.right = TreeNode.new(7)
	tree2.root.left.right.left.right.right = TreeNode.new(1)
	#  Test
	tree1.isSkewedBinaryTree()
	tree2.isSkewedBinaryTree()
end

main()

input

 Inorder Traversal 
  8  9  7  1  6  4
 Is Skewed Binary Tree 

 Inorder Traversal 
  8  9  7  1  6  3  4
 Is Not Skewed Binary Tree 
/*
    Scala Program
    Check whether given binary is skewed binary tree 
*/

// Binary Tree node
class TreeNode(var data: Int , var left: TreeNode , var right: TreeNode)
{
	def this(data: Int)
	{
		// Set node value
		this(data,null,null);
	}
}
class BinaryTree(var root: TreeNode)
{
	def this()
	{
		this(null);
	}
	// Recursive inorder traversal of binary tree
	def inorder(node: TreeNode): Unit = {
		if (node != null)
		{
			inorder(node.left);
			print("  " + node.data);
			inorder(node.right);
		}
	}
	// Determine that tree is skewed tree or not
	def skewedTree(node: TreeNode): Boolean = {
		if (node == null)
		{
			return true;
		}
		if (node.left != null && node.right != null)
		{
			// When node contains both child
			return false;
		}
		// Recursively check skewed tree
		return skewedTree(node.left) && skewedTree(node.right);
	}
	def isSkewedBinaryTree(): Unit = {
		if (this.root == null)
		{
			// When tree is empty
			print("\n Empty Tree");
		}
		else
		{
			// Display given tree node
			println("\n Inorder Traversal ");
			inorder(this.root);
			if (skewedTree(root) == true)
			{
				println("\n Is Skewed Binary Tree ");
			}
			else
			{
				println("\n Is Not Skewed Binary Tree ");
			}
		}
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		// Create new binary trees
		var tree1: BinaryTree = new BinaryTree();
		var tree2: BinaryTree = new BinaryTree();
		/*
		    Create First Binary Tree
		    4
		   /   
		  8    
		   \  
		    6 
		   /   
		  9  
		   \
		    7
		     \
		      1  
		*/
		// Add tree node
		tree1.root = new TreeNode(4);
		tree1.root.left = new TreeNode(8);
		tree1.root.left.right = new TreeNode(6);
		tree1.root.left.right.left = new TreeNode(9);
		tree1.root.left.right.left.right = new TreeNode(7);
		tree1.root.left.right.left.right.right = new TreeNode(1);
		/*
		    Create Second Binary Tree 
		    4
		   /   
		  8    
		   \  
		    6 
		   / \ 
		  9   3
		   \
		    7
		     \
		      1  
		*/
		// Add tree node
		tree2.root = new TreeNode(4);
		tree2.root.left = new TreeNode(8);
		tree2.root.left.right = new TreeNode(6);
		tree2.root.left.right.right = new TreeNode(3);
		tree2.root.left.right.left = new TreeNode(9);
		tree2.root.left.right.left.right = new TreeNode(7);
		tree2.root.left.right.left.right.right = new TreeNode(1);
		// Test
		tree1.isSkewedBinaryTree();
		tree2.isSkewedBinaryTree();
	}
}

input

 Inorder Traversal
  8  9  7  1  6  4
 Is Skewed Binary Tree

 Inorder Traversal
  8  9  7  1  6  3  4
 Is Not Skewed Binary Tree
/*
    Swift 4 Program
    Check whether given binary is skewed binary tree 
*/
//Binary Tree node
class TreeNode
{
	var data: Int;
	var left: TreeNode? ;
	var right: TreeNode? ;
	init(_ data: Int)
	{
		// Set node value
		self.data = data;
		self.left = nil;
		self.right = nil;
	}
}
class BinaryTree
{
	var root: TreeNode? ;
	init()
	{
		self.root = nil;
	}
	// Recursive inorder traversal of binary tree
	func inorder(_ node: TreeNode? )
	{
		if (node  != nil)
		{
			self.inorder(node!.left);
			print("  ", node!.data, terminator: "");
			self.inorder(node!.right);
		}
	}
	// Determine that tree is skewed tree or not
	func skewedTree(_ node: TreeNode? )->Bool
	{
		if (node == nil)
		{
			return true;
		}
		if (node!.left  != nil && node!.right  != nil)
		{
			// When node contains both child
			return false;
		}
		// Recursively check skewed tree
		return self.skewedTree(node!.left) && self.skewedTree(node!.right);
	}
	func isSkewedBinaryTree()
	{
		if (self.root == nil)
		{
			// When tree is empty
			print("\n Empty Tree", terminator: "");
		}
		else
		{
			// Display given tree node
			print("\n Inorder Traversal ");
			self.inorder(self.root);
			if (self.skewedTree(self.root) == true)
			{
				print("\n Is Skewed Binary Tree ");
			}
			else
			{
				print("\n Is Not Skewed Binary Tree ");
			}
		}
	}
}
func main()
{
	// Create new binary trees
	let tree1: BinaryTree = BinaryTree();
	let tree2: BinaryTree = BinaryTree();
	/*
	    Create First Binary Tree
	    4
	   /   
	  8    
	   \  
	    6 
	   /   
	  9  
	   \
	    7
	     \
	      1  
	*/
	// Add tree node
	tree1.root = TreeNode(4);
	tree1.root!.left = TreeNode(8);
	tree1.root!.left!.right = TreeNode(6);
	tree1.root!.left!.right!.left = TreeNode(9);
	tree1.root!.left!.right!.left!.right = TreeNode(7);
	tree1.root!.left!.right!.left!.right!.right = TreeNode(1);
	/*
	    Create Second Binary Tree 
	    4
	   /   
	  8    
	   \  
	    6 
	   / \ 
	  9   3
	   \
	    7
	     \
	      1  
	*/
	// Add tree node
	tree2.root = TreeNode(4);
	tree2.root!.left = TreeNode(8);
	tree2.root!.left!.right = TreeNode(6);
	tree2.root!.left!.right!.right = TreeNode(3);
	tree2.root!.left!.right!.left = TreeNode(9);
	tree2.root!.left!.right!.left!.right = TreeNode(7);
	tree2.root!.left!.right!.left!.right!.right = TreeNode(1);
	// Test
	tree1.isSkewedBinaryTree();
	tree2.isSkewedBinaryTree();
}
main();

input

 Inorder Traversal
   8   9   7   1   6   4
 Is Skewed Binary Tree

 Inorder Traversal
   8   9   7   1   6   3   4
 Is Not Skewed Binary Tree
/*
    Kotlin Program
    Check whether given binary is skewed binary tree 
*/

//Binary Tree node
class TreeNode
{
	var data: Int;
	var left: TreeNode ? ;
	var right: TreeNode ? ;
	constructor(data: Int)
	{
		// Set node value
		this.data = data;
		this.left = null;
		this.right = null;
	}
}
class BinaryTree
{
	var root: TreeNode ? ;
	constructor()
	{
		this.root = null;
	}
	// Recursive inorder traversal of binary tree
	fun inorder(node: TreeNode ? ): Unit
	{
		if (node != null)
		{
			this.inorder(node.left);
			print("  " + node.data);
			this.inorder(node.right);
		}
	}
	// Determine that tree is skewed tree or not
	fun skewedTree(node: TreeNode ? ): Boolean
	{
		if (node == null)
		{
			return true;
		}
		if (node.left != null && node.right != null)
		{
			// When node contains both child
			return false;
		}
		// Recursively check skewed tree
		return this.skewedTree(node.left) && this.skewedTree(node.right);
	}
	fun isSkewedBinaryTree(): Unit
	{
		if (this.root == null)
		{
			// When tree is empty
			print("\n Empty Tree");
		}
		else
		{
			// Display given tree node
			println("\n Inorder Traversal ");
			this.inorder(this.root);
			if (this.skewedTree(this.root) == true)
			{
				println("\n Is Skewed Binary Tree ");
			}
			else
			{
				println("\n Is Not Skewed Binary Tree ");
			}
		}
	}
}
fun main(args: Array < String > ): Unit
{
	// Create new binary trees
	val tree1: BinaryTree = BinaryTree();
	val tree2: BinaryTree = BinaryTree();
	/*
	    Create First Binary Tree
	    4
	   /   
	  8    
	   \  
	    6 
	   /   
	  9  
	   \
	    7
	     \
	      1  
	*/
	// Add tree node
	tree1.root = TreeNode(4);
	tree1.root?.left = TreeNode(8);
	tree1.root?.left?.right = TreeNode(6);
	tree1.root?.left?.right?.left = TreeNode(9);
	tree1.root?.left?.right?.left?.right = TreeNode(7);
	tree1.root?.left?.right?.left?.right?.right = TreeNode(1);
	/*
	    Create Second Binary Tree 
	    4
	   /   
	  8    
	   \  
	    6 
	   / \ 
	  9   3
	   \
	    7
	     \
	      1  
	*/
	// Add tree node
	tree2.root = TreeNode(4);
	tree2.root?.left = TreeNode(8);
	tree2.root?.left?.right = TreeNode(6);
	tree2.root?.left?.right?.right = TreeNode(3);
	tree2.root?.left?.right?.left = TreeNode(9);
	tree2.root?.left?.right?.left?.right = TreeNode(7);
	tree2.root?.left?.right?.left?.right?.right = TreeNode(1);
	// Test
	tree1.isSkewedBinaryTree();
	tree2.isSkewedBinaryTree();
}

input

 Inorder Traversal
  8  9  7  1  6  4
 Is Skewed Binary Tree

 Inorder Traversal
  8  9  7  1  6  3  4
 Is Not Skewed Binary Tree


Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment







© 2021, kalkicode.com, All rights reserved