Sum of nodes in binary tree whose grandparents exists

Here given code implementation process.

// C program for
// Sum of nodes in binary tree whose grandparents exists
#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 *newTree()
{
	// Create a dynamic tree 
	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 *getNode(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;
}
void preOrder(struct TreeNode *node)
{
	if (node != NULL)
	{
		// Print node value
		printf("  %d", node->data);
		// Visit left subtree
		preOrder(node->left);
		// Visit right subtree
		preOrder(node->right);
	}
}
int grandchildSum(struct TreeNode *node, 
                  struct TreeNode *parent, struct TreeNode *grandParent)
{
	if (node != NULL)
	{
		int result = 0;
		if (parent != NULL && grandParent != NULL)
		{
			// Print grandchild node
			printf("  %d", node->data);
			// Get the node
			result = node->data;
		}
		// Visit left subtree and right subtree
		return result + grandchildSum(node->left, node, parent) + grandchildSum(node->right, node, parent);
	}
	else
	{
		return 0;
	}
}
int main(int argc, char
	const *argv[])
{
	struct BinaryTree *tree = newTree();
	/*
	         4                            
	       /   \    
	      12    7    
	     / \      \               
	    2   3      1
	       / \    / 
	      6   8  5
	     /        
	    9          
	                   
	-----------------
	  Constructing binary tree

	*/
	tree->root = getNode(4);
	tree->root->left = getNode(12);
	tree->root->left->right = getNode(3);
	tree->root->left->right->left = getNode(6);
	tree->root->left->right->left->left = getNode(9);
	tree->root->left->right->right = getNode(8);
	tree->root->left->left = getNode(2);
	tree->root->right = getNode(7);
	tree->root->right->right = getNode(1);
	tree->root->right->right->left = getNode(5);
	printf("\n  Tree Nodes ");
	preOrder(tree->root);
	printf("\n  Grandchild Node is :");
	int result = grandchildSum(tree->root, NULL, NULL);
	printf("\n  Sum of Grandchild is : %d\n", result);
	return 0;
}

input

  Tree Nodes   4  12  2  3  6  9  8  7  1  5
  Grandchild Node is :  2  3  6  9  8  1  5
  Sum of Grandchild is : 34
/*
    Java Program
    Sum of nodes in binary tree whose grandparents exists
*/
// 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;
	}
	public void preOrder(TreeNode node)
	{
		if (node != null)
		{
			// Print node value
			System.out.print(" " + node.data);
			// Visit left subtree
			preOrder(node.left);
			// Visit right subtree
			preOrder(node.right);
		}
	}
	public int grandchildSum(TreeNode node, 
                              TreeNode parent, TreeNode grandParent)
	{
		if (node != null)
		{
			int result = 0;
			if (parent != null && grandParent != null)
			{
				// Print grandchild node
				System.out.print(" " + node.data);
				// Get the node
				result = node.data;
			}
			// Visit left subtree and right subtree
			return result + grandchildSum(node.left, node, parent) 
              + grandchildSum(node.right, node, parent);
		}
		else
		{
			return 0;
		}
	}
	public static void main(String[] args)
	{
		// Create new binary trees 
		BinaryTree tree = new BinaryTree();
		/*
		         4                            
		       /   \    
		      12    7    
		     / \      \               
		    2   3      1
		       / \    / 
		      6   8  5
		     /        
		    9          
		                   
		-----------------
		  Constructing binary tree
		*/
		tree.root = new TreeNode(4);
		tree.root.left = new TreeNode(12);
		tree.root.left.right = new TreeNode(3);
		tree.root.left.right.left = new TreeNode(6);
		tree.root.left.right.left.left = new TreeNode(9);
		tree.root.left.right.right = new TreeNode(8);
		tree.root.left.left = new TreeNode(2);
		tree.root.right = new TreeNode(7);
		tree.root.right.right = new TreeNode(1);
		tree.root.right.right.left = new TreeNode(5);
		System.out.print("\n Tree Nodes ");
		tree.preOrder(tree.root);
		System.out.print("\n Grandchild Node is :");
		int result = tree.grandchildSum(tree.root, null, null);
		System.out.print("\n Sum of Grandchild is : " + result + "\n");
	}
}

input

 Tree Nodes  4 12 2 3 6 9 8 7 1 5
 Grandchild Node is : 2 3 6 9 8 1 5
 Sum of Grandchild is : 34
// Include header file
#include <iostream>
using namespace std;

/*
    C++ Program
    Sum of nodes in binary tree whose grandparents exists
*/

// 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;
	}
	void preOrder(TreeNode *node)
	{
		if (node != NULL)
		{
			// Print node value
			cout << " " << node->data;
			// Visit left subtree
			this->preOrder(node->left);
			// Visit right subtree
			this->preOrder(node->right);
		}
	}
	int grandchildSum(TreeNode *node, 
                      TreeNode *parent, TreeNode *grandParent)
	{
		if (node != NULL)
		{
			int result = 0;
			if (parent != NULL && grandParent != NULL)
			{
				// Print grandchild node
				cout << " " << node->data;
				// Get the node
				result = node->data;
			}
			// Visit left subtree and right subtree
			return result + this->grandchildSum(node->left, node, parent) 
              + this->grandchildSum(node->right, node, parent);
		}
		else
		{
			return 0;
		}
	}
};
int main()
{
	// Create new binary trees
	BinaryTree *tree = new BinaryTree();
	/*
	         4                            
	       /   \    
	      12    7    
	     / \      \               
	    2   3      1
	       / \    / 
	      6   8  5
	     /        
	    9          
	                   
	-----------------
	  Constructing binary tree
	*/
	tree->root = new TreeNode(4);
	tree->root->left = new TreeNode(12);
	tree->root->left->right = new TreeNode(3);
	tree->root->left->right->left = new TreeNode(6);
	tree->root->left->right->left->left = new TreeNode(9);
	tree->root->left->right->right = new TreeNode(8);
	tree->root->left->left = new TreeNode(2);
	tree->root->right = new TreeNode(7);
	tree->root->right->right = new TreeNode(1);
	tree->root->right->right->left = new TreeNode(5);
	cout << "\n Tree Nodes ";
	tree->preOrder(tree->root);
	cout << "\n Grandchild Node is :";
	int result = tree->grandchildSum(tree->root, NULL, NULL);
	cout << "\n Sum of Grandchild is : " << result << "\n";
	return 0;
}

input

 Tree Nodes  4 12 2 3 6 9 8 7 1 5
 Grandchild Node is : 2 3 6 9 8 1 5
 Sum of Grandchild is : 34
// Include namespace system
using System;
/*
    Csharp Program
    Sum of nodes in binary tree whose grandparents exists
*/
// 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;
	}
	public void preOrder(TreeNode node)
	{
		if (node != null)
		{
			// Print node value
			Console.Write(" " + node.data);
			// Visit left subtree
			this.preOrder(node.left);
			// Visit right subtree
			this.preOrder(node.right);
		}
	}
	public int grandchildSum(TreeNode node, 
                              TreeNode parent, TreeNode grandParent)
	{
		if (node != null)
		{
			int result = 0;
			if (parent != null && grandParent != null)
			{
				// Print grandchild node
				Console.Write(" " + node.data);
				// Get the node
				result = node.data;
			}
			// Visit left subtree and right subtree
			return result + this.grandchildSum(node.left, node, parent) 
              + this.grandchildSum(node.right, node, parent);
		}
		else
		{
			return 0;
		}
	}
	public static void Main(String[] args)
	{
		// Create new binary trees
		BinaryTree tree = new BinaryTree();
		/*
		         4                            
		       /   \    
		      12    7    
		     / \      \               
		    2   3      1
		       / \    / 
		      6   8  5
		     /        
		    9          
		                   
		-----------------
		  Constructing binary tree
		*/
		tree.root = new TreeNode(4);
		tree.root.left = new TreeNode(12);
		tree.root.left.right = new TreeNode(3);
		tree.root.left.right.left = new TreeNode(6);
		tree.root.left.right.left.left = new TreeNode(9);
		tree.root.left.right.right = new TreeNode(8);
		tree.root.left.left = new TreeNode(2);
		tree.root.right = new TreeNode(7);
		tree.root.right.right = new TreeNode(1);
		tree.root.right.right.left = new TreeNode(5);
		Console.Write("\n Tree Nodes ");
		tree.preOrder(tree.root);
		Console.Write("\n Grandchild Node is :");
		int result = tree.grandchildSum(tree.root, null, null);
		Console.Write("\n Sum of Grandchild is : " + result + "\n");
	}
}

input

 Tree Nodes  4 12 2 3 6 9 8 7 1 5
 Grandchild Node is : 2 3 6 9 8 1 5
 Sum of Grandchild is : 34
<?php
/*
    Php Program
    Sum of nodes in binary tree whose grandparents exists
*/
// 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;
	}
	public	function preOrder($node)
	{
		if ($node != NULL)
		{
			// Print node value
			echo(" ".$node->data);
			// Visit left subtree
			$this->preOrder($node->left);
			// Visit right subtree
			$this->preOrder($node->right);
		}
	}
	public	function grandchildSum($node, $parent, $grandParent)
	{
		if ($node != NULL)
		{
			$result = 0;
			if ($parent != NULL && $grandParent != NULL)
			{
				// Print grandchild node
				echo(" ".$node->data);
				// Get the node
				$result = $node->data;
			}
			// Visit left subtree and right subtree
			return $result + $this->grandchildSum($node->left, $node, $parent) 
              + $this->grandchildSum($node->right, $node, $parent);
		}
		else
		{
			return 0;
		}
	}
}

function main()
{
	// Create new binary trees
	$tree = new BinaryTree();
	/*
	         4                            
	       /   \    
	      12    7    
	     / \      \               
	    2   3      1
	       / \    / 
	      6   8  5
	     /        
	    9          
	                   
	-----------------
	  Constructing binary tree
	*/
	$tree->root = new TreeNode(4);
	$tree->root->left = new TreeNode(12);
	$tree->root->left->right = new TreeNode(3);
	$tree->root->left->right->left = new TreeNode(6);
	$tree->root->left->right->left->left = new TreeNode(9);
	$tree->root->left->right->right = new TreeNode(8);
	$tree->root->left->left = new TreeNode(2);
	$tree->root->right = new TreeNode(7);
	$tree->root->right->right = new TreeNode(1);
	$tree->root->right->right->left = new TreeNode(5);
	echo("\n Tree Nodes ");
	$tree->preOrder($tree->root);
	echo("\n Grandchild Node is :");
	$result = $tree->grandchildSum($tree->root, NULL, NULL);
	echo("\n Sum of Grandchild is : ".$result.
		"\n");
}
main();

input

 Tree Nodes  4 12 2 3 6 9 8 7 1 5
 Grandchild Node is : 2 3 6 9 8 1 5
 Sum of Grandchild is : 34
/*
    Node JS Program
    Sum of nodes in binary tree whose grandparents exists
*/
// 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;
	}
	preOrder(node)
	{
		if (node != null)
		{
			// Print node value
			process.stdout.write(" " + node.data);
			// Visit left subtree
			this.preOrder(node.left);
			// Visit right subtree
			this.preOrder(node.right);
		}
	}
	grandchildSum(node, parent, grandParent)
	{
		if (node != null)
		{
			var result = 0;
			if (parent != null && grandParent != null)
			{
				// Print grandchild node
				process.stdout.write(" " + node.data);
				// Get the node
				result = node.data;
			}
			// Visit left subtree and right subtree
			return result + this.grandchildSum(node.left, node, parent) 
              + this.grandchildSum(node.right, node, parent);
		}
		else
		{
			return 0;
		}
	}
}

function main()
{
	// Create new binary trees
	var tree = new BinaryTree();
	/*
	         4                            
	       /   \    
	      12    7    
	     / \      \               
	    2   3      1
	       / \    / 
	      6   8  5
	     /        
	    9          
	                   
	-----------------
	  Constructing binary tree
	*/
	tree.root = new TreeNode(4);
	tree.root.left = new TreeNode(12);
	tree.root.left.right = new TreeNode(3);
	tree.root.left.right.left = new TreeNode(6);
	tree.root.left.right.left.left = new TreeNode(9);
	tree.root.left.right.right = new TreeNode(8);
	tree.root.left.left = new TreeNode(2);
	tree.root.right = new TreeNode(7);
	tree.root.right.right = new TreeNode(1);
	tree.root.right.right.left = new TreeNode(5);
	process.stdout.write("\n Tree Nodes ");
	tree.preOrder(tree.root);
	process.stdout.write("\n Grandchild Node is :");
	var result = tree.grandchildSum(tree.root, null, null);
	process.stdout.write("\n Sum of Grandchild is : " + result + "\n");
}
main();

input

 Tree Nodes  4 12 2 3 6 9 8 7 1 5
 Grandchild Node is : 2 3 6 9 8 1 5
 Sum of Grandchild is : 34
#    Python 3 Program
#    Sum of nodes in binary tree whose grandparents exists

#  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
	
	def preOrder(self, node) :
		if (node != None) :
			#  Print node value
			print(" ", node.data, end = "")
			#  Visit left subtree
			self.preOrder(node.left)
			#  Visit right subtree
			self.preOrder(node.right)
		
	
	def grandchildSum(self, node, parent, grandParent) :
		if (node != None) :
			result = 0
			if (parent != None and grandParent != None) :
				#  Print grandchild node
				print(" ", node.data, end = "")
				#  Get the node
				result = node.data
			
			#  Visit left subtree and right subtree
			return result + self.grandchildSum(
              node.left, node, parent
            ) + self.grandchildSum(node.right, node, parent)
		else :
			return 0
		
	

def main() :
	#  Create new binary trees
	tree = BinaryTree()
	#         4                            
	#       /   \    
	#      12    7    
	#     / \      \               
	#    2   3      1
	#       / \    / 
	#      6   8  5
	#     /        
	#    9          
	# -----------------
	#  Constructing binary tree
	tree.root = TreeNode(4)
	tree.root.left = TreeNode(12)
	tree.root.left.right = TreeNode(3)
	tree.root.left.right.left = TreeNode(6)
	tree.root.left.right.left.left = TreeNode(9)
	tree.root.left.right.right = TreeNode(8)
	tree.root.left.left = TreeNode(2)
	tree.root.right = TreeNode(7)
	tree.root.right.right = TreeNode(1)
	tree.root.right.right.left = TreeNode(5)
	print("\n Tree Nodes ", end = "")
	tree.preOrder(tree.root)
	print("\n Grandchild Node is :", end = "")
	result = tree.grandchildSum(tree.root, None, None)
	print("\n Sum of Grandchild is : ", result )

if __name__ == "__main__": main()

input

 Tree Nodes   4  12  2  3  6  9  8  7  1  5
 Grandchild Node is :  2  3  6  9  8  1  5
 Sum of Grandchild is :  34
#    Ruby Program
#    Sum of nodes in binary tree whose grandparents exists

#  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

	def preOrder(node) 
		if (node != nil) 
			#  Print node value
			print(" ", node.data)
			#  Visit left subtree
			self.preOrder(node.left)
			#  Visit right subtree
			self.preOrder(node.right)
		end

	end

	def grandchildSum(node, parent, grandParent) 
		if (node != nil) 
			result = 0
			if (parent != nil && grandParent != nil) 
				#  Print grandchild node
				print(" ", node.data)
				#  Get the node
				result = node.data
			end

			#  Visit left subtree and right subtree
			return result + self.grandchildSum(node.left, node, parent) +
              self.grandchildSum(node.right, node, parent)
		else
 
			return 0
		end

	end

end

def main() 
	#  Create new binary trees
	tree = BinaryTree.new()
	#         4                            
	#       /   \    
	#      12    7    
	#     / \      \               
	#    2   3      1
	#       / \    / 
	#      6   8  5
	#     /        
	#    9          
	# -----------------
	#  Constructing binary tree
	tree.root = TreeNode.new(4)
	tree.root.left = TreeNode.new(12)
	tree.root.left.right = TreeNode.new(3)
	tree.root.left.right.left = TreeNode.new(6)
	tree.root.left.right.left.left = TreeNode.new(9)
	tree.root.left.right.right = TreeNode.new(8)
	tree.root.left.left = TreeNode.new(2)
	tree.root.right = TreeNode.new(7)
	tree.root.right.right = TreeNode.new(1)
	tree.root.right.right.left = TreeNode.new(5)
	print("\n Tree Nodes ")
	tree.preOrder(tree.root)
	print("\n Grandchild Node is :")
	result = tree.grandchildSum(tree.root, nil, nil)
	print("\n Sum of Grandchild is : ", result ,"\n")
end

main()

input

 Tree Nodes  4 12 2 3 6 9 8 7 1 5
 Grandchild Node is : 2 3 6 9 8 1 5
 Sum of Grandchild is : 34
/*
    Scala Program
    Sum of nodes in binary tree whose grandparents exists
*/
// 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);
	}
	def preOrder(node: TreeNode): Unit = {
		if (node != null)
		{
			// Print node value
			print(" " + node.data);
			// Visit left subtree
			preOrder(node.left);
			// Visit right subtree
			preOrder(node.right);
		}
	}
	def grandchildSum(node: TreeNode, 
                      parent: TreeNode, grandParent: TreeNode): Int = {
		if (node != null)
		{
			var result: Int = 0;
			if (parent != null && grandParent != null)
			{
				// Print grandchild node
				print(" " + node.data);
				// Get the node
				result = node.data;
			}
			// Visit left subtree and right subtree
			return result + grandchildSum(node.left, node, parent) + 
              grandchildSum(node.right, node, parent);
		}
		else
		{
			return 0;
		}
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		// Create new binary trees
		var tree: BinaryTree = new BinaryTree();
		/*
		         4                            
		       /   \    
		      12    7    
		     / \      \               
		    2   3      1
		       / \    / 
		      6   8  5
		     /        
		    9          
		                   
		-----------------
		  Constructing binary tree
		*/
		tree.root = new TreeNode(4);
		tree.root.left = new TreeNode(12);
		tree.root.left.right = new TreeNode(3);
		tree.root.left.right.left = new TreeNode(6);
		tree.root.left.right.left.left = new TreeNode(9);
		tree.root.left.right.right = new TreeNode(8);
		tree.root.left.left = new TreeNode(2);
		tree.root.right = new TreeNode(7);
		tree.root.right.right = new TreeNode(1);
		tree.root.right.right.left = new TreeNode(5);
		print("\n Tree Nodes ");
		tree.preOrder(tree.root);
		print("\n Grandchild Node is :");
		var result: Int = tree.grandchildSum(tree.root, null, null);
		print("\n Sum of Grandchild is : " + result + "\n");
	}
}

input

 Tree Nodes  4 12 2 3 6 9 8 7 1 5
 Grandchild Node is : 2 3 6 9 8 1 5
 Sum of Grandchild is : 34
/*
    Swift 4 Program
    Sum of nodes in binary tree whose grandparents exists
*/
// 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;
	}
	func preOrder(_ node: TreeNode? )
	{
		if (node  != nil)
		{
			// Print node value
			print(" ", node!.data, terminator: "");
			// Visit left subtree
			self.preOrder(node!.left);
			// Visit right subtree
			self.preOrder(node!.right);
		}
	}
	func grandchildSum(_ node: TreeNode? , 
                       _ parent : TreeNode? , _ grandParent : TreeNode? ) -> Int
	{
		if (node  != nil)
		{
			var result = 0;
			if (parent  != nil && grandParent  != nil)
			{
				// Print grandchild node
				print(" ", node!.data, terminator: "");
				// Get the node
				result = node!.data;
			}
			// Visit left subtree and right subtree
			return result + self.grandchildSum(node!.left, node, parent) 
              + self.grandchildSum(node!.right, node, parent);
		}
		else
		{
			return 0;
		}
	}
}
func main()
{
	// Create new binary trees
	let tree = BinaryTree();
	/*
	         4                            
	       /   \    
	      12    7    
	     / \      \               
	    2   3      1
	       / \    / 
	      6   8  5
	     /        
	    9          
	                   
	-----------------
	  Constructing binary tree
	*/
	tree.root = TreeNode(4);
	tree.root!.left = TreeNode(12);
	tree.root!.left!.right = TreeNode(3);
	tree.root!.left!.right!.left = TreeNode(6);
	tree.root!.left!.right!.left!.left = TreeNode(9);
	tree.root!.left!.right!.right = TreeNode(8);
	tree.root!.left!.left = TreeNode(2);
	tree.root!.right = TreeNode(7);
	tree.root!.right!.right = TreeNode(1);
	tree.root!.right!.right!.left = TreeNode(5);
	print("\n Tree Nodes ", terminator: "");
	tree.preOrder(tree.root);
	print("\n Grandchild Node is :", terminator: "");
	let result = tree.grandchildSum(tree.root, nil, nil);
	print("\n Sum of Grandchild is : ", result );
}
main();

input

 Tree Nodes   4  12  2  3  6  9  8  7  1  5
 Grandchild Node is :  2  3  6  9  8  1  5
 Sum of Grandchild is :  34
/*
    Kotlin Program
    Sum of nodes in binary tree whose grandparents exists
*/
// 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;
	}
	fun preOrder(node: TreeNode ? ): Unit
	{
		if (node != null)
		{
			// Print node value
			print(" " + node.data);
			// Visit left subtree
			this.preOrder(node.left);
			// Visit right subtree
			this.preOrder(node.right);
		}
	}
	fun grandchildSum(node: TreeNode ? , 
                      parent : TreeNode ? , grandParent : TreeNode ? ): Int
	{
		if (node != null)
		{
			var result: Int = 0;
			if (parent != null && grandParent != null)
			{
				// Print grandchild node
				print(" " + node.data);
				// Get the node
				result = node.data;
			}
			// Visit left subtree and right subtree
			return result + this.grandchildSum(node.left, node, parent) + 
              this.grandchildSum(node.right, node, parent);
		}
		else
		{
			return 0;
		}
	}
}
fun main(args: Array < String > ): Unit
{
	// Create new binary trees
	val tree: BinaryTree = BinaryTree();
	/*
	         4                            
	       /   \    
	      12    7    
	     / \      \               
	    2   3      1
	       / \    / 
	      6   8  5
	     /        
	    9          
	                   
	-----------------
	  Constructing binary tree
	*/
	tree.root = TreeNode(4);
	tree.root?.left = TreeNode(12);
	tree.root?.left?.right = TreeNode(3);
	tree.root?.left?.right?.left = TreeNode(6);
	tree.root?.left?.right?.left?.left = TreeNode(9);
	tree.root?.left?.right?.right = TreeNode(8);
	tree.root?.left?.left = TreeNode(2);
	tree.root?.right = TreeNode(7);
	tree.root?.right?.right = TreeNode(1);
	tree.root?.right?.right?.left = TreeNode(5);
	print("\n Tree Nodes ");
	tree.preOrder(tree.root);
	print("\n Grandchild Node is :");
	val result: Int = tree.grandchildSum(tree.root, null, null);
	print("\n Sum of Grandchild is : " + result + "\n");
}

input

 Tree Nodes  4 12 2 3 6 9 8 7 1 5
 Grandchild Node is : 2 3 6 9 8 1 5
 Sum of Grandchild is : 34


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