Count odd paths in Binary Tree

Here given code implementation process.

// C program 
// Count odd paths in Binary Tree

#include <stdio.h>
#include <stdlib.h>

//Binary Tree node
struct Node
{
    int data;
    struct Node *left, *right;
};

//This is creating a binary tree node and return this
struct Node *get_node(int data)
{
    // Create dynamic node
    struct Node *new_node = (struct Node *) malloc(sizeof(struct Node));
    if (new_node != NULL)
    {
        //Set data and pointer values
        new_node->data = data;
        new_node->left = NULL;
        new_node->right = NULL;
    }
    else
    {
        //This is indicates, segmentation fault or memory overflow problem
        printf("Memory Overflow\n");
    }
    //return new node
    return new_node;
}

// Count all paths from root to leaf which containing all Odd nodes
int count_odd_path(struct Node*root)
{

    if(root == NULL || root->data % 2 == 0)
    {
        //When tree node is null or its contain Even value
        return 0;
    }
    else
    {
        if(root->left==NULL && root->right == NULL)
        {
            //When get leaf node
            return 1;
        }
        return count_odd_path(root->left) + count_odd_path(root->right);
    }
}

int main()
{
   
    struct Node *root = NULL;
 
    /* 
    Construct Binary Tree
    -----------------------
             5
           /    \
          3      9
         / \    / \
        7   6  1   3
       / \    / \   
      10  3  7   4
    */

    // Add tree node
    root = get_node(5);
    root->left = get_node(3);
    root->right = get_node(9);
    root->right->right = get_node(3);
    root->left->right = get_node(6);
    root->right->left = get_node(1);
    root->left->left = get_node(7);
    root->left->left->left = get_node(10);
    root->left->left->right = get_node(3);
    root->right->left->right = get_node(4);
    root->right->left->left = get_node(7);

    int counter = count_odd_path(root);

    printf("Total Odd path in this tree are : %d \n",counter);    

    return 0;
}

Output

Total Odd path in this tree are : 3
/* 
  Java Program 
  Count odd paths in Binary Tree
*/

//Binary Tree node
class Node
{
	public int data;
	public Node left;
	public Node right;
	public Node(int data)
	{
		// Set node value
		this.data = data;
		this.left = null;
		this.right = null;
	}
}
class BinaryTree
{
	public Node root;
	public BinaryTree()
	{
		//Set initial tree root to null
		this.root = null;
	}
	// Count all paths from root to leaf which containing all Odd nodes
	public int count_odd_path(Node root)
	{
		if (root == null || root.data % 2 == 0)
		{
			//When tree node is null or its contain Even value
			return 0;
		}
		else
		{
			if (root.left == null && root.right == null)
			{
				//When get leaf node
				return 1;
			}
			return count_odd_path(root.left) + count_odd_path(root.right);
		}
	}
	public static void main(String[] args)
	{
		//Make object of binary tree
		BinaryTree tree = new BinaryTree();
		/* 
		Construct Binary Tree
		-----------------------
		         5
		       /    \
		      3      9
		     / \    / \
		    7   6  1   3
		   / \    / \   
		  10  3  7   4
		*/
		//Add tree node
		tree.root = new Node(5);
		tree.root.left = new Node(3);
		tree.root.right = new Node(9);
		tree.root.right.right = new Node(3);
		tree.root.left.right = new Node(6);
		tree.root.right.left = new Node(1);
		tree.root.left.left = new Node(7);
		tree.root.left.left.left = new Node(10);
		tree.root.left.left.right = new Node(3);
		tree.root.right.left.right = new Node(4);
		tree.root.right.left.left = new Node(7);
		
		int counter = tree.count_odd_path(tree.root);
		// Display calculated result
		System.out.print("Total Odd path in this tree are : " + counter + " \n");
	}
}

Output

Total Odd path in this tree are : 3
//Include header file
#include <iostream>
using namespace std;

/*
  C++ Program 
  Count odd paths in Binary Tree
*/

//Binary Tree node
class Node
{
	public: 
    int data;
	Node *left;
	Node *right;
	Node(int data)
	{
		// Set node value
		this->data = data;
		this->left = NULL;
		this->right = NULL;
	}
};
class BinaryTree
{
	public: 
    Node *root;
	BinaryTree()
	{
		//Set initial tree root to null
		this->root = NULL;
	}
	// Count all paths from root to leaf which containing all Odd nodes
	int count_odd_path(Node *root)
	{
		if (root == NULL || root->data % 2 == 0)
		{
			//When tree node is null or its contain Even value
			return 0;
		}
		else
		{
			if (root->left == NULL && root->right == NULL)
			{
				//When get leaf node
				return 1;
			}
			return this->count_odd_path(root->left) + this->count_odd_path(root->right);
		}
	}
};
int main()
{
	//Make object of binary tree
	BinaryTree tree = BinaryTree();
	/*
			Construct Binary Tree
			-----------------------
			         5
			       /    \
			      3      9
			     / \    / \
			    7   6  1   3
			   / \    / \   
			  10  3  7   4
			*/
	//Add tree node
	tree.root = new Node(5);
	tree.root->left = new Node(3);
	tree.root->right = new Node(9);
	tree.root->right->right = new Node(3);
	tree.root->left->right = new Node(6);
	tree.root->right->left = new Node(1);
	tree.root->left->left = new Node(7);
	tree.root->left->left->left = new Node(10);
	tree.root->left->left->right = new Node(3);
	tree.root->right->left->right = new Node(4);
	tree.root->right->left->left = new Node(7);
	int counter = tree.count_odd_path(tree.root);
	// Display calculated result
	cout << "Total Odd path in this tree are : " << counter << " \n";
	return 0;
}

Output

Total Odd path in this tree are : 3
//Include namespace system
using System;

/* 
  C# Program 
  Count odd paths in Binary Tree
*/

//Binary Tree node
class Node
{
	public int data;
	public Node left;
	public Node right;
	public Node(int data)
	{
		// Set node value
		this.data = data;
		this.left = null;
		this.right = null;
	}
}
class BinaryTree
{
	public Node root;
	public BinaryTree()
	{
		//Set initial tree root to null
		this.root = null;
	}
	// Count all paths from root to leaf which containing all Odd nodes
	public int count_odd_path(Node root)
	{
		if (root == null || root.data % 2 == 0)
		{
			//When tree node is null or its contain Even value
			return 0;
		}
		else
		{
			if (root.left == null && root.right == null)
			{
				//When get leaf node
				return 1;
			}
			return count_odd_path(root.left) + count_odd_path(root.right);
		}
	}
	public static void Main(String[] args)
	{
		//Make object of binary tree
		BinaryTree tree = new BinaryTree();
		/* 
				Construct Binary Tree
				-----------------------
				         5
				       /    \
				      3      9
				     / \    / \
				    7   6  1   3
				   / \    / \   
				  10  3  7   4
				*/
		//Add tree node
		tree.root = new Node(5);
		tree.root.left = new Node(3);
		tree.root.right = new Node(9);
		tree.root.right.right = new Node(3);
		tree.root.left.right = new Node(6);
		tree.root.right.left = new Node(1);
		tree.root.left.left = new Node(7);
		tree.root.left.left.left = new Node(10);
		tree.root.left.left.right = new Node(3);
		tree.root.right.left.right = new Node(4);
		tree.root.right.left.left = new Node(7);
		int counter = tree.count_odd_path(tree.root);
		// Display calculated result
		Console.Write("Total Odd path in this tree are : " + counter + " \n");
	}
}

Output

Total Odd path in this tree are : 3
<?php
/* 
  Php Program 
  Count odd paths in Binary Tree
*/

// Binary Tree node
class Node
{
	public $data;
	public $left;
	public $right;

	function __construct($data)
	{
		// Set node value
		$this->data = $data;
		$this->left = null;
		$this->right = null;
	}
}
class BinaryTree
{
	public $root;

	function __construct()
	{
		//Set initial tree root to null
		$this->root = null;
	}
	// Count all paths from root to leaf which containing all Odd nodes
	public	function count_odd_path($root)
	{
		if ($root == null || $root->data % 2 == 0)
		{
			//When tree node is null or its contain Even value
			return 0;
		}
		else
		{
			if ($root->left == null && $root->right == null)
			{
				//When get leaf node
				return 1;
			}
			return $this->count_odd_path($root->left) + $this->count_odd_path($root->right);
		}
	}
}

function main()
{
	//Make object of binary tree
	$tree = new BinaryTree();
	/* 
			Construct Binary Tree
			-----------------------
			         5
			       /    \
			      3      9
			     / \    / \
			    7   6  1   3
			   / \    / \   
			  10  3  7   4
			*/
	//Add tree node
	$tree->root = new Node(5);
	$tree->root->left = new Node(3);
	$tree->root->right = new Node(9);
	$tree->root->right->right = new Node(3);
	$tree->root->left->right = new Node(6);
	$tree->root->right->left = new Node(1);
	$tree->root->left->left = new Node(7);
	$tree->root->left->left->left = new Node(10);
	$tree->root->left->left->right = new Node(3);
	$tree->root->right->left->right = new Node(4);
	$tree->root->right->left->left = new Node(7);
	$counter = $tree->count_odd_path($tree->root);
	// Display calculated result
	echo "Total Odd path in this tree are : ". $counter ." \n";
}
main();

Output

Total Odd path in this tree are : 3
/* 
  Node Js Program 
  Count odd paths in Binary Tree
*/

//Binary Tree node
class Node
{
	constructor(data)
	{
		// Set node value
		this.data = data;
		this.left = null;
		this.right = null;
	}
}

class BinaryTree
{
	constructor()
	{
		//Set initial tree root to null
		this.root = null;
	}
	// Count all paths from root to leaf which containing all Odd nodes
	count_odd_path(root)
	{
		if (root == null || root.data % 2 == 0)
		{
			//When tree node is null or its contain Even value
			return 0;
		}
		else
		{
			if (root.left == null && root.right == null)
			{
				//When get leaf node
				return 1;
			}
			return this.count_odd_path(root.left) + this.count_odd_path(root.right);
		}
	}
}

function main()
{
	//Make object of binary tree
	var tree = new BinaryTree();
	/* 
			Construct Binary Tree
			-----------------------
			         5
			       /    \
			      3      9
			     / \    / \
			    7   6  1   3
			   / \    / \   
			  10  3  7   4
			*/
	//Add tree node
	tree.root = new Node(5);
	tree.root.left = new Node(3);
	tree.root.right = new Node(9);
	tree.root.right.right = new Node(3);
	tree.root.left.right = new Node(6);
	tree.root.right.left = new Node(1);
	tree.root.left.left = new Node(7);
	tree.root.left.left.left = new Node(10);
	tree.root.left.left.right = new Node(3);
	tree.root.right.left.right = new Node(4);
	tree.root.right.left.left = new Node(7);
	var counter = tree.count_odd_path(tree.root);
	// Display calculated result
	process.stdout.write("Total Odd path in this tree are : " + counter + " \n");
}
main();

Output

Total Odd path in this tree are : 3
#   Python 3 Program 
#   Count odd paths in Binary Tree

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

class BinaryTree :
	
	def __init__(self) :
		# Set initial tree root to null
		self.root = None
	
	#  Count all paths from root to leaf which containing all Odd nodes
	def count_odd_path(self, root) :
		if (root == None or root.data % 2 == 0) :
			# When tree node is null or its contain Even value
			return 0
		else :
			if (root.left == None and root.right == None) :
				# When get leaf node
				return 1
			
			return self.count_odd_path(root.left) + self.count_odd_path(root.right)
		
	

def main() :
	# Make object of binary tree
	tree = BinaryTree()
	#  
	# 		Construct Binary Tree
	# 		-----------------------
	# 		         5
	# 		       /    \
	# 		      3      9
	# 		     / \    / \
	# 		    7   6  1   3
	# 		   / \    / \   
	# 		  10  3  7   4
	# 		
	
	# Add tree node
	tree.root = Node(5)
	tree.root.left = Node(3)
	tree.root.right = Node(9)
	tree.root.right.right = Node(3)
	tree.root.left.right = Node(6)
	tree.root.right.left = Node(1)
	tree.root.left.left = Node(7)
	tree.root.left.left.left = Node(10)
	tree.root.left.left.right = Node(3)
	tree.root.right.left.right = Node(4)
	tree.root.right.left.left = Node(7)
	counter = tree.count_odd_path(tree.root)
	#  Display calculated result
	print("Total Odd path in this tree are : ", counter ," \n", end = "")

if __name__ == "__main__": main()

Output

Total Odd path in this tree are :  3
#   Ruby Program 
#   Count odd paths in Binary Tree

# Binary Tree node
class Node  
	# Define the accessor and reader of class Node  
	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() 
		# Set initial tree root to null
		self.root = nil
	end

	#  Count all paths from root to leaf which containing all Odd nodes
	def count_odd_path(root) 
		if (root == nil || root.data % 2 == 0) 
			# When tree node is null or its contain Even value
			return 0
		else 
			if (root.left == nil && root.right == nil) 
				# When get leaf node
				return 1
			end

			return self.count_odd_path(root.left) + self.count_odd_path(root.right)
		end

	end

end

def main() 
	# Make object of binary tree
	tree = BinaryTree.new()
	#  
	# 		Construct Binary Tree
	# 		-----------------------
	# 		         5
	# 		       /    \
	# 		      3      9
	# 		     / \    / \
	# 		    7   6  1   3
	# 		   / \    / \   
	# 		  10  3  7   4
	# 		
	
	# Add tree node
	tree.root = Node.new(5)
	tree.root.left = Node.new(3)
	tree.root.right = Node.new(9)
	tree.root.right.right = Node.new(3)
	tree.root.left.right = Node.new(6)
	tree.root.right.left = Node.new(1)
	tree.root.left.left = Node.new(7)
	tree.root.left.left.left = Node.new(10)
	tree.root.left.left.right = Node.new(3)
	tree.root.right.left.right = Node.new(4)
	tree.root.right.left.left = Node.new(7)
	counter = tree.count_odd_path(tree.root)
	#  Display calculated result
	print("Total Odd path in this tree are : ", counter ," \n")
end

main()

Output

Total Odd path in this tree are : 3 
/* 
  Scala Program 
  Count odd paths in Binary Tree
*/

//Binary Tree node
class Node(var data: Int,
	var left: Node,
		var right: Node)
{
	def this(data: Int)
	{
		this(data, null, null);
	}
}
class BinaryTree(var root: Node)
{
	def this()
	{
		this(null);
	}
	// Count all paths from root to leaf which containing all Odd nodes
	def count_odd_path(root: Node): Int = {
		if (root == null || root.data % 2 == 0)
		{
			//When tree node is null or its contain Even value
			return 0;
		}
		else
		{
			if (root.left == null && root.right == null)
			{
				//When get leaf node
				return 1;
			}
			return count_odd_path(root.left) + count_odd_path(root.right);
		}
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		//Make object of binary tree
		var tree: BinaryTree = new BinaryTree();
		/* 
				Construct Binary Tree
				-----------------------
				         5
				       /    \
				      3      9
				     / \    / \
				    7   6  1   3
				   / \    / \   
				  10  3  7   4
				*/
		//Add tree node
		tree.root = new Node(5);
		tree.root.left = new Node(3);
		tree.root.right = new Node(9);
		tree.root.right.right = new Node(3);
		tree.root.left.right = new Node(6);
		tree.root.right.left = new Node(1);
		tree.root.left.left = new Node(7);
		tree.root.left.left.left = new Node(10);
		tree.root.left.left.right = new Node(3);
		tree.root.right.left.right = new Node(4);
		tree.root.right.left.left = new Node(7);
		var counter: Int = tree.count_odd_path(tree.root);
		// Display calculated result
		print("Total Odd path in this tree are : " + counter + " \n");
	}
}

Output

Total Odd path in this tree are : 3
/* 
  Swift 4 Program 
  Count odd paths in Binary Tree
*/

//Binary Tree node
class Node
{
	var data: Int;
	var left: Node? ;
	var right: Node? ;
	init(_ data: Int)
	{
		// Set node value
		self.data = data;
		self.left = nil;
		self.right = nil;
	}
}
class BinaryTree
{
	var root: Node? ;
	init()
	{
		//Set initial tree root to null
		self.root = nil;
	}
	// Count all paths from root to leaf which containing all Odd nodes
	func count_odd_path(_ root: Node? ) -> Int
	{
		if (root == nil || root!.data % 2 == 0)
		{
			//When tree node is null or its contain Even value
			return 0;
		}
		else
		{
			if (root!.left == nil && root!.right == nil)
			{
				//When get leaf node
				return 1;
			}
			return self.count_odd_path(root!.left) + self.count_odd_path(root!.right);
		}
	}
}
func main()
{
	//Make object of binary tree
	let tree: BinaryTree = BinaryTree();
	/* 
			Construct Binary Tree
			-----------------------
			         5
			       /    \
			      3      9
			     / \    / \
			    7   6  1   3
			   / \    / \   
			  10  3  7   4
	*/
	//Add tree node
	tree.root = Node(5);
	tree.root!.left = Node(3);
	tree.root!.right = Node(9);
	tree.root!.right!.right = Node(3);
	tree.root!.left!.right = Node(6);
	tree.root!.right!.left = Node(1);
	tree.root!.left!.left = Node(7);
	tree.root!.left!.left!.left = Node(10);
	tree.root!.left!.left!.right = Node(3);
	tree.root!.right!.left!.right = Node(4);
	tree.root!.right!.left!.left = Node(7);
	let counter: Int = tree.count_odd_path(tree.root);
	// Display calculated result
	print("Total Odd path in this tree are : ", counter ," \n", terminator: "");
}
main();

Output

Total Odd path in this tree are :  3


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