Sum of leaf nodes at minimum level

Sum of maximum depth nodes in tree

Here given code implementation process.

/*
  C Program 
+ Sum of leaf nodes at minimum level

*/
#include <stdio.h>

#include <stdlib.h>
 //structure of Binary Tree node
struct Node
{
	int data;
	struct Node *left, *right;
};
//Create a binary tree nodes and node fields (data,pointer) 
//And returning the reference of newly nodes
struct Node *insert(int data)
{
	//create dynamic memory to new binary tree 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; //Initially node left-pointer is NULL
		new_node->right = NULL; //Initially node right-pointer is NULL
	}
	else
	{
		printf("Memory Overflow\n");
		exit(0); //Terminate program execution
	}
	//return reference
	return new_node;
}
//Find the minimum level leaf node height
void find_height(struct Node *root, int *height, int level)
{
	if (root != NULL)
	{
		if ((root->left == NULL && root->right == NULL) && ( *height == -1 || ( *height) > level))
		{
			*height = level;
		}
		find_height(root->left, height, level + 1);
		find_height(root->right, height, level + 1);
	}
}
//Calculate sum of all leaf nodes at minimum level
int min_level_leaf_sum(struct Node *root, int level, int k)
{
	if (root != NULL && level <= k)
	{
		if (level == k && root->left == NULL && root->right == NULL)
		{
			//When get a leaf nodes are at minimum level
			return root->data;
		}
		return min_level_leaf_sum(root->left, level + 1, k) + min_level_leaf_sum(root->right, level + 1, k);
	}
	else
	{
		return 0;
	}
}
//This function are handle the request of find the sum of leaf at min depth 
void min_depth(struct Node *root)
{
	int height = -1;
	//Find the min level leaf node height
	find_height(root, & height, 0);
	int sum = min_level_leaf_sum(root, 0, height);
	printf(" Minimum level leaf nodes sum : %d \n", sum);
}
int main()
{
	struct Node *root = NULL;
	/* Make A Binary Tree
	-----------------------
	        1
	       /  \
	      2    3
	     /    /  \
	    3    1    1
	             / \
	            4   6
	           /
	          7
	*/
	//Insertion of binary tree nodes
	root = insert(1);
	root->left = insert(2);
	root->right = insert(3);
	root->right->right = insert(1);
	root->right->left = insert(1);
	root->left->left = insert(3);
	root->right->right->left = insert(4);
	root->right->right->right = insert(6);
	root->right->right->left->left = insert(7);
	min_depth(root);
	return 0;
}

Output

 Minimum level leaf nodes sum : 4
/* 
  Java Program 
  Sum of leaf nodes at minimum level
 */
//Class of Binary Tree node
class Node
{
	public int data;
	public Node left;
	public Node right;
	//make a tree node
	public Node(int data)
	{
		//assign field values
		this.data = data;
		this.left = null;
		this.right = null;
	}
}
class BinaryTree
{
	public Node root;
  	public int height;
	public BinaryTree()
	{
		//set initial tree root to null
		root = null;
        height = 0;
	}
	//Find the minimum level leaf node height
	public void find_height(Node root, int level)
	{
		if (root != null)
		{
			if ((root.left == null && root.right == null) && (this.height == 1 || (this.height) > level))
			{
				this.height = level;
			}
			find_height(root.left, level + 1);
			find_height(root.right, level + 1);
		}
	}
	//Calculate sum of all leaf nodes at minimum level
	public int min_level_leaf_sum(Node root, int level)
	{
		if (root != null && level <= this.height)
		{
			if (level == this.height && root.left == null && root.right == null)
			{
				//When get a leaf nodes are at minimum level
				return root.data;
			}
			return min_level_leaf_sum(root.left, level + 1) + min_level_leaf_sum(root.right, level + 1);
		}
		else
		{
			return 0;
		}
	}
	//This function are handle the request of find the sum of leaf at min depth 
	public void min_depth()
	{
      	if(this.root==null)
        {
          return;
        }
		this.height = 1;
		//Find the min level leaf node height
		find_height(this.root, 0);
		int sum = min_level_leaf_sum(root, 0);
		System.out.print(" Minimum level leaf nodes sum is : " + sum + " \n");
	}
	public static void main(String[] args)
	{
		//Make object of Binary Tree
		BinaryTree obj = new BinaryTree();
		/*  Make A Binary Tree
		-----------------------
		        1
		       /  \
		      2    3
		     /    /  \
		    3    1    1
		             / \
		            4   6
		           /
		          7
		*/
		//Insertion of binary tree nodes
		obj.root = new Node(1);
		obj.root.left = new Node(2);
		obj.root.right = new Node(3);
		obj.root.right.right = new Node(1);
		obj.root.right.left = new Node(1);
		obj.root.left.left = new Node(3);
		obj.root.right.right.left = new Node(4);
		obj.root.right.right.right = new Node(6);
		obj.root.right.right.left.left = new Node(7);
		obj.min_depth();
	}
}

Output

 Minimum level leaf nodes sum is : 4
/* 
  C++ Program 
  Sum of leaf nodes at minimum level
 */
//Class of Binary Tree node
#include<iostream>

using namespace std;
class Node
{
	public: 
    int data;
	Node * left;
	Node * right;
	//make a tree node
	Node(int data)
	{
		
		//assign field values
		this-> data = data;
		this->left = NULL;
		this->right = NULL;
	}
};
class BinaryTree
{
	public: Node * root;
	int height;
	BinaryTree()
	{
		//set initial tree root to null
		this->root = NULL;
		this->height = 0;
	}
	//Find the minimum level leaf node height
	void find_height(Node * root, int level)
	{
		if (root != NULL)
		{
			if ((root->left == NULL && root->right == NULL) && (this->height == 1 || (this->height) > level))
			{
				this->height = level;
			}
			find_height(root->left, level + 1);
			find_height(root->right, level + 1);
		}
	}
	//Calculate sum of all leaf nodes at minimum level
	int min_level_leaf_sum(Node * root, int level)
	{
		if (root != NULL && level <= this->height)
		{
			if (level == this->height && root->left == NULL && root->right == NULL)
			{
				
				//When get a leaf nodes are at minimum level
				return root->data;
			}
			return min_level_leaf_sum(root->left, level + 1) + min_level_leaf_sum(root->right, level + 1);
		}
		else
		{
			return 0;
		}
	}
	//This function are handle the request of find the sum of leaf at min depth 
	void min_depth()
	{
		if (this->root == NULL)
		{
			return;
		}
		this->height = 1;
		//Find the min level leaf node height
		find_height(this->root, 0);
		int sum = min_level_leaf_sum(this->root, 0);
		cout << " Minimum level leaf nodes sum is : " << sum << " \n";
	}
};
int main()
{
	//Make object of Binary Tree
	BinaryTree obj =  BinaryTree();
	/*  Make A Binary Tree
			-----------------------
			        1
			       /  \
			      2    3
			     /    /  \
			    3    1    1
			             / \
			            4   6
			           /
			          7
			*/
	//Insertion of binary tree nodes
	obj.root = new Node(1);
	obj.root->left = new Node(2);
	obj.root->right = new Node(3);
	obj.root->right->right = new Node(1);
	obj.root->right->left = new Node(1);
	obj.root->left->left = new Node(3);
	obj.root->right->right->left = new Node(4);
	obj.root->right->right->right = new Node(6);
	obj.root->right->right->left->left = new Node(7);
	obj.min_depth();
	return 0;
}

Output

 Minimum level leaf nodes sum is : 4
/* 
  C# Program 
  Sum of leaf nodes at minimum level
 */
//Class of Binary Tree node
using System;
class Node
{
	public int data;
	public Node left;
	public Node right;
	//make a tree node
	public Node(int data)
	{
		//assign field values
		this.data = data;
		this.left = null;
		this.right = null;
	}
}
class BinaryTree
{
	public Node root;
	public int height;
	public BinaryTree()
	{
		//set initial tree root to null
		root = null;
		height = 0;
	}
	//Find the minimum level leaf node height
	public void find_height(Node root, int level)
	{
		if (root != null)
		{
			if ((root.left == null && root.right == null) && (this.height == 1 || (this.height) > level))
			{
				this.height = level;
			}
			find_height(root.left, level + 1);
			find_height(root.right, level + 1);
		}
	}
	//Calculate sum of all leaf nodes at minimum level
	public int min_level_leaf_sum(Node root, int level)
	{
		if (root != null && level <= this.height)
		{
			if (level == this.height && root.left == null && root.right == null)
			{
				return root.data;
			}
			return min_level_leaf_sum(root.left, level + 1) + min_level_leaf_sum(root.right, level + 1);
		}
		else
		{
			return 0;
		}
	}
	//This function are handle the request of find the sum of leaf at min depth 
	public void min_depth()
	{
		if (this.root == null)
		{
			return;
		}
		this.height = 1;
		//Find the min level leaf node height
		find_height(this.root, 0);
		int sum = min_level_leaf_sum(root, 0);
		Console.Write(" Minimum level leaf nodes sum is : " + sum + " \n");
	}
	public static void Main(String[] args)
	{
		//Make object of Binary Tree
		BinaryTree obj = new BinaryTree();
		/*  Make A Binary Tree
				-----------------------
				        1
				       /  \
				      2    3
				     /    /  \
				    3    1    1
				             / \
				            4   6
				           /
				          7
				*/
		//Insertion of binary tree nodes
		obj.root = new Node(1);
		obj.root.left = new Node(2);
		obj.root.right = new Node(3);
		obj.root.right.right = new Node(1);
		obj.root.right.left = new Node(1);
		obj.root.left.left = new Node(3);
		obj.root.right.right.left = new Node(4);
		obj.root.right.right.right = new Node(6);
		obj.root.right.right.left.left = new Node(7);
		obj.min_depth();
	}
}

Output

 Minimum level leaf nodes sum is : 4
<?php
/* 
  Php Program 
  Sum of leaf nodes at minimum level
 */
//Class of Binary Tree node
class Node
{
	public $data;
	public $left;
	public $right;
	//make a tree node
	function __construct($data)
	{
		//assign field values
		$this->data = $data;
		$this->left = null;
		$this->right = null;
	}
}
class BinaryTree
{
	public $root;
	public $height;

	function __construct()
	{
		//set initial tree root to null
		$this->root = null;
		$this->height = 0;
	}
	//Find the minimum level leaf node height
	public	function find_height($root, $level)
	{
		if ($root != null)
		{
			if (($root->left == null && $root->right == null) && ($this->height == 1 || ($this->height) > $level))
			{
				$this->height = $level;
			}
			$this->find_height($root->left, $level + 1);
			$this->find_height($root->right, $level + 1);
		}
	}
	//Calculate sum of all leaf nodes at minimum level
	public	function min_level_leaf_sum($root, $level)
	{
		if ($root != null && $level <= $this->height)
		{
			if ($level == $this->height && $root->left == null && $root->right == null)
			{
				return $root->data;
			}
			return $this->min_level_leaf_sum($root->left, $level + 1) + $this->min_level_leaf_sum($root->right, $level + 1);
		}
		else
		{
			return 0;
		}
	}
	//This function are handle the request of find the sum of leaf at min depth 
	public	function min_depth()
	{
		if ($this->root == null)
		{
			return;
		}
		$this->height = 1;
		//Find the min level leaf node height
		$this->find_height($this->root, 0);
		$sum = $this->min_level_leaf_sum($this->root, 0);
		echo " Minimum level leaf nodes sum is : ". $sum ." \n";
	}
}

function main()
{
	//Make object of Binary Tree
	$obj = new BinaryTree();
	/*  Make A Binary Tree
			-----------------------
			        1
			       /  \
			      2    3
			     /    /  \
			    3    1    1
			             / \
			            4   6
			           /
			          7
			*/
	//Insertion of binary tree nodes

	$obj->root = new Node(1);
	$obj->root->left = new Node(2);
	$obj->root->right = new Node(3);
	$obj->root->right->right = new Node(1);
	$obj->root->right->left = new Node(1);
	$obj->root->left->left = new Node(3);
	$obj->root->right->right->left = new Node(4);
	$obj->root->right->right->right = new Node(6);
	$obj->root->right->right->left->left = new Node(7);
	$obj->min_depth();
}
main();

Output

 Minimum level leaf nodes sum is : 4
/* 
  Node Js Program 
  Sum of leaf nodes at minimum level
 */
//Class of Binary Tree node
class Node
{
	//make a tree node
	constructor(data)
	{
		//assign field values
		this.data = data;
		this.left = null;
		this.right = null;
	}
}
class BinaryTree
{
	constructor()
	{
		//set initial tree root to null
		this.root = null;
		this.height = 0;
	}
	//Find the minimum level leaf node height
	find_height(root, level)
	{
		if (root != null)
		{
			if ((root.left == null && root.right == null) && (this.height == 1 || (this.height) > level))
			{
				this.height = level;
			}
			this.find_height(root.left, level + 1);
			this.find_height(root.right, level + 1);
		}
	}
	//Calculate sum of all leaf nodes at minimum level
	min_level_leaf_sum(root, level)
	{
		if (root != null && level <= this.height)
		{
			if (level == this.height && root.left == null && root.right == null)
			{
				return root.data;
			}
			return this.min_level_leaf_sum(root.left, level + 1) + this.min_level_leaf_sum(root.right, level + 1);
		}
		else
		{
			return 0;
		}
	}
	//This function are handle the request of find the sum of leaf at min depth 
	min_depth()
	{
		if (this.root == null)
		{
			return;
		}
		this.height = 1;
		//Find the min level leaf node height
		this.find_height(this.root, 0);
		var sum = this.min_level_leaf_sum(this.root, 0);
		process.stdout.write(" Minimum level leaf nodes sum is : " + sum + " \n");
	}
}

function main()
{
	//Make object of Binary Tree
	var obj = new BinaryTree();
	/*  Make A Binary Tree
			-----------------------
			        1
			       /  \
			      2    3
			     /    /  \
			    3    1    1
			             / \
			            4   6
			           /
			          7
			*/
	//Insertion of binary tree nodes
	//Insertion of binary tree nodes
	obj.root = new Node(1);
	obj.root.left = new Node(2);
	obj.root.right = new Node(3);
	obj.root.right.right = new Node(1);
	obj.root.right.left = new Node(1);
	obj.root.left.left = new Node(3);
	obj.root.right.right.left = new Node(4);
	obj.root.right.right.right = new Node(6);
	obj.root.right.right.left.left = new Node(7);
	obj.min_depth();
}
main();

Output

 Minimum level leaf nodes sum is : 4
#   Python 3 Program 
#   Sum of leaf nodes at minimum level

# Class of Binary Tree node
class Node :
	
	# make a tree node
	def __init__(self, data) :
		# assign field values
		self.data = data
		self.left = None
		self.right = None
	

class BinaryTree :
	
	def __init__(self) :
		# set initial tree root to null
		self.root = None
		self.height = 0
	
	# Find the minimum level leaf node height
	def find_height(self, root, level) :
		if (root != None) :
			if ((root.left == None and root.right == None) and(self.height == 1 or(self.height) > level)) :
				self.height = level
			
			self.find_height(root.left, level + 1)
			self.find_height(root.right, level + 1)
		
	
	# Calculate sum of all leaf nodes at minimum level
	def min_level_leaf_sum(self, root, level) :
		if (root != None and level <= self.height) :
			if (level == self.height and root.left == None and root.right == None) :
				
				# When get a leaf nodes are at minimum level
				return root.data
			
			return self.min_level_leaf_sum(root.left, level + 1) + self.min_level_leaf_sum(root.right, level + 1)
		else :
			return 0
		
	
	# This function are handle the request of find the sum of leaf at min depth 
	def min_depth(self) :
		if (self.root == None) :
			return
		
		self.height = 1
		# Find the min level leaf node height
		self.find_height(self.root, 0)
		sum = self.min_level_leaf_sum(self.root, 0)
		print(" Minimum level leaf nodes sum is : ", sum ," \n", end = "")
	

def main() :
	# Make object of Binary Tree
	obj = BinaryTree()
	#   Make A Binary Tree
	# 		-----------------------
	# 		        1
	# 		       /  \
	# 		      2    3
	# 		     /    /  \
	# 		    3    1    1
	# 		             / \
	# 		            4   6
	# 		           /
	# 		          7
	# 		
	
	# Insertion of binary tree nodes
	obj.root = Node(1)
	obj.root.left = Node(2)
	obj.root.right = Node(3)
	obj.root.right.right = Node(1)
	obj.root.right.left = Node(1)
	obj.root.left.left = Node(3)
	obj.root.right.right.left = Node(4)
	obj.root.right.right.right = Node(6)
	obj.root.right.right.left.left = Node(7)
	obj.min_depth()

if __name__ == "__main__": main()

Output

 Minimum level leaf nodes sum is :  4
#   Ruby Program 
#   Sum of leaf nodes at minimum level
 
# Class of Binary Tree node
class Node 

	# Define the accessor and reader of class Node  
	attr_reader :data, :left, :right
	attr_accessor :data, :left, :right
	
	# make a tree node
	def initialize(data)
	
		# assign field values
		self.data = data
		self.left = nil
		self.right = nil
	end
end
class BinaryTree 

	# Define the accessor and reader of class BinaryTree  
	attr_reader :root, :height
	attr_accessor :root, :height
    
	def initialize()
	
		# set initial tree root to null
		@root = nil
		@height = 0
	end
	# Find the minimum level leaf node height
	def find_height(root, level)
	
		if (root != nil)
		
			if ((root.left == nil && root.right == nil) && (self.height == 1 || (self.height) > level))
			
				self.height = level
			end
			self.find_height(root.left, level + 1)
			self.find_height(root.right, level + 1)
		end
	end
	# Calculate sum of all leaf nodes at minimum level
	def min_level_leaf_sum(root, level)
	
		if (root != nil && level <= self.height)
		
			if (level == self.height && root.left == nil && root.right == nil)
				# When get a leaf nodes are at minimum level
				return root.data
			end
			return self.min_level_leaf_sum(root.left, level + 1) + self.min_level_leaf_sum(root.right, level + 1)
		else
		
			return 0
		end
	end
	# This function are handle the request of find the sum of leaf at min depth 
	def min_depth()
	
		if (self.root == nil)
			return
		end
		self.height = 1
		# Find the min level leaf node height
		self.find_height(self.root, 0)
		sum = self.min_level_leaf_sum(@root, 0)
		print(" Minimum level leaf nodes sum is : ", sum ," \n")
	end
end
def main()

	# Make object of Binary Tree
	obj = BinaryTree.new()
	#   Make A Binary Tree
	# 		-----------------------
	# 		        1
	# 		       /  \
	# 		      2    3
	# 		     /    /  \
	# 		    3    1    1
	# 		             / \
	# 		            4   6
	# 		           /
	# 		          7
	# 		
	
	# Insertion of binary tree nodes
	obj.root = Node.new(1)
	obj.root.left = Node.new(2)
	obj.root.right = Node.new(3)
	obj.root.right.right = Node.new(1)
	obj.root.right.left = Node.new(1)
	obj.root.left.left = Node.new(3)
	obj.root.right.right.left = Node.new(4)
	obj.root.right.right.right = Node.new(6)
	obj.root.right.right.left.left = Node.new(7)
	obj.min_depth()
end
main()

Output

 Minimum level leaf nodes sum is : 4 
/* 
  Scala Program 
  Sum of leaf nodes at minimum level
 */
//Class of Binary Tree node
class Node(var data: Int,
	var left: Node,
		var right: Node)
{
	//make a tree node
	def this(data: Int)
	{
		//assign field values
		this(data,null,null)
	}
}
class BinaryTree(var root: Node,
	var height: Int)
{
	def this()
	{
		//set initial tree root to null
		this(null,0);
	}
	//Find the minimum level leaf node height
	def find_height(root: Node, level: Int): Unit = {
		if (root != null)
		{
			if ((root.left == null && root.right == null) && (this.height == 1 || (this.height) > level))
			{
				this.height = level;
			}
			find_height(root.left, level + 1);
			find_height(root.right, level + 1);
		}
	}
	//Calculate sum of all leaf nodes at minimum level
	def min_level_leaf_sum(root: Node, level: Int): Int = {
		if (root != null && level <= this.height)
		{
			if (level == this.height && root.left == null && root.right == null)
			{
				return root.data;
			}
			return min_level_leaf_sum(root.left, level + 1) + min_level_leaf_sum(root.right, level + 1);
		}
		else
		{
			return 0;
		}
	}
	//This function are handle the request of find the sum of leaf at min depth 
	def min_depth(): Unit = {
		if (this.root == null)
		{
			return;
		}
		this.height = 1;
		//Find the min level leaf node height
		find_height(this.root, 0);
		var sum: Int = min_level_leaf_sum(root, 0);
		print(" Minimum level leaf nodes sum is : " + sum + " \n");
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		//Make object of Binary Tree
		var obj: BinaryTree = new BinaryTree();
		/*  Make A Binary Tree
				-----------------------
				        1
				       /  \
				      2    3
				     /    /  \
				    3    1    1
				             / \
				            4   6
				           /
				          7
				*/
		//Insertion of binary tree nodes
		obj.root = new Node(1);
		obj.root.left = new Node(2);
		obj.root.right = new Node(3);
		obj.root.right.right = new Node(1);
		obj.root.right.left = new Node(1);
		obj.root.left.left = new Node(3);
		obj.root.right.right.left = new Node(4);
		obj.root.right.right.right = new Node(6);
		obj.root.right.right.left.left = new Node(7);
		obj.min_depth();
	}
}

Output

 Minimum level leaf nodes sum is : 4
/* 
  Swift Program 
  Sum of leaf nodes at minimum level
 */
//Class of Binary Tree node
class Node
{
	var data: Int;
	var left: Node? ;
	var right: Node? ;
	//make a tree node
	init(_ data: Int)
	{
		//assign field values
		self.data = data;
		self.left = nil;
		self.right = nil;
	}
}
class BinaryTree
{
	var root: Node? ;
	var height: Int;
	init()
	{
		//set initial tree root to null
		self.root = nil;
		self.height = 0;
	}
	//Find the minimum level leaf node height
	func find_height(_ root: Node? , _ level : Int)
	{
		if (root != nil)
		{
			if ((root!.left == nil && root!.right == nil) && (self.height == 1 || (self.height) > level))
			{
				self.height = level;
			}
			self.find_height(root!.left, level + 1);
			self.find_height(root!.right, level + 1);
		}
	}
	//Calculate sum of all leaf nodes at minimum level
	func min_level_leaf_sum(_ root: Node? , _ level : Int) -> Int
	{
		if (root != nil && level <= self.height)
		{
			if (level == self.height && root!.left == nil && root!.right == nil)
			{
				
				//When get a leaf nodes are at minimum level
				return root!.data;
			}
			return self.min_level_leaf_sum(root!.left, level + 1) + self.min_level_leaf_sum(root!.right, level + 1);
		}
		else
		{
			return 0;
		}
	}
	//This function are handle the request of find the sum of leaf at min depth 
	func min_depth()
	{
		if (self.root == nil)
		{
			return;
		}
		self.height = 1;
		//Find the min level leaf node height
		self.find_height(self.root, 0);
		let sum: Int = self.min_level_leaf_sum(self.root, 0);
		print(" Minimum level leaf nodes sum is : ", sum ," \n", terminator: "");
	}
}
func main()
{
	//Make object of Binary Tree
	let obj: BinaryTree = BinaryTree();
	/*  Make A Binary Tree
			-----------------------
			        1
			       /  \
			      2    3
			     /    /  \
			    3    1    1
			             / \
			            4   6
			           /
			          7
			*/
	//Insertion of binary tree nodes
	obj.root = Node(1);
	obj.root!.left = Node(2);
	obj.root!.right = Node(3);
	obj.root!.right!.right = Node(1);
	obj.root!.right!.left = Node(1);
	obj.root!.left!.left = Node(3);
	obj.root!.right!.right!.left = Node(4);
	obj.root!.right!.right!.right = Node(6);
	obj.root!.right!.right!.left!.left = Node(7);
	obj.min_depth();
}
main();

Output

 Minimum level leaf nodes sum is :  4


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