Posted on by Kalkicode
Code Binary Tree

Find the sum of the node having child node x

Here given code implementation process.

/*
    C Program 
    Find the sum of the node having child node x
    Recursive solution
*/
#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;
}
//Display pre order elements
void preorder(struct Node *node)
{
	if (node != NULL)
	{
		//Print node value
		printf("  %d", node->data);
		preorder(node->left);
		preorder(node->right);
	}
}
// Returns the sum of all parent that child  contains x values
int parent_sum_of_x(struct Node *node, int x)
{
	int sum = 0;
	if (node != NULL)
	{
		if (node->left != NULL && node->left->data == x)
		{
			sum = node->data;
		}
		else if (node->right != NULL && node->right->data == x)
		{
			sum = node->data;
		}
		// Find the x node parent in left and right subtree
		sum = sum + parent_sum_of_x(node->left, x) + parent_sum_of_x(node->right, x);
	}
	return sum;
}
int main()
{
	struct Node *root = NULL;
	/*
	constructor binary tree
	-----------------------

	      6                            
	   /    \    
	  7      3     
	 / \      \               
	1   10     1
	   /  \   / \
	  1    1 1   5

	-----------------------
	*/
	root = get_node(6);
	root->left = get_node(7);
	root->left->left = get_node(1);
	root->left->right = get_node(10);
	root->left->right->right = get_node(1);
	root->left->right->left = get_node(1);
	root->right = get_node(3);
	root->right->right = get_node(1);
	root->right->right->right = get_node(5);
	root->right->right->left = get_node(1);
	printf("\n Tree Nodes : ");
	preorder(root);
	int x = 1;
	//Display calculated result
	printf("\n Sum of parent node of [%d] is  : %d\n", x, parent_sum_of_x(root, x));
	return 0;
}

Output

 Tree Nodes :   6  7  1  10  1  1  3  1  1  5
 Sum of parent node of [1] is  : 21
/*
    Java Program 
    Find the sum of the node having child node x
    Recursive solution
*/

//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;
    }
}
public class BinaryTree
{
    public Node root;
    public BinaryTree()
    {
        //Set initial tree root to null
        this.root = null;
    }
    //Display pre order elements
    public void preorder(Node node)
    {
        if (node != null)
        {
            //Print node value
            System.out.print("  " + node.data);
            preorder(node.left);
            preorder(node.right);
        }
    }
    // Returns the sum of all parent that child  contains x values
    public int parent_sum_of_x(Node node, int x)
    {
        int sum = 0;
        if (node != null)
        {
            if (node.left != null && node.left.data == x)
            {
                sum = node.data;
            }
            else if (node.right != null && node.right.data == x)
            {
                sum = node.data;
            }
            // Find the x node parent in left and right subtree
            sum = sum + parent_sum_of_x(node.left, x) + parent_sum_of_x(node.right, x);
        }
        return sum;
    }
    public static void main(String[] args)
    {
        //Make object of binary tree
        BinaryTree tree = new BinaryTree();
        /*
            constructor binary tree
            -----------------------

                  6                            
               /    \    
              7      3     
             / \      \               
            1   10     1
               /  \   / \
              1    1 1   5

            -----------------------
            */
        tree.root = new Node(6);
        tree.root.left = new Node(7);
        tree.root.left.left = new Node(1);
        tree.root.left.right = new Node(10);
        tree.root.left.right.right = new Node(1);
        tree.root.left.right.left = new Node(1);
        tree.root.right = new Node(3);
        tree.root.right.right = new Node(1);
        tree.root.right.right.right = new Node(5);
        tree.root.right.right.left = new Node(1);
        System.out.print("\n Tree Nodes : ");
        tree.preorder(tree.root);
        int x = 1;
        //Display calculated result
        System.out.print("\n Sum of parent node of [" + x + "] is : " + tree.parent_sum_of_x(tree.root, x) + "\n");
    }
}

Output

 Tree Nodes :   6  7  1  10  1  1  3  1  1  5
 Sum of parent node of [1] is : 21
//Include header file
#include <iostream>
using namespace std;

/*
    C++ Program 
    Find the sum of the node having child node x
    Recursive solution
*/

//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;
    }
    //Display pre order elements
    void preorder(Node *node)
    {
        if (node != NULL)
        {
            //Print node value
            cout << "  " << node->data;
            this->preorder(node->left);
            this->preorder(node->right);
        }
    }
    // Returns the sum of all parent that child  contains x values
    int parent_sum_of_x(Node *node, int x)
    {
        int sum = 0;
        if (node != NULL)
        {
            if (node->left != NULL && node->left->data == x)
            {
                sum = node->data;
            }
            else if (node->right != NULL && node->right->data == x)
            {
                sum = node->data;
            }
            // Find the x node parent in left and right subtree
            sum = sum + this->parent_sum_of_x(node->left, x) + this->parent_sum_of_x(node->right, x);
        }
        return sum;
    }
};
int main()
{
    //Make object of binary tree
    BinaryTree tree = BinaryTree();
    /*
    constructor binary tree
    -----------------------

          6                            
       /    \    
      7      3     
     / \      \               
    1   10     1
       /  \   / \
      1    1 1   5

     -----------------------
    */
    tree.root = new Node(6);
    tree.root->left = new Node(7);
    tree.root->left->left = new Node(1);
    tree.root->left->right = new Node(10);
    tree.root->left->right->right = new Node(1);
    tree.root->left->right->left = new Node(1);
    tree.root->right = new Node(3);
    tree.root->right->right = new Node(1);
    tree.root->right->right->right = new Node(5);
    tree.root->right->right->left = new Node(1);
    cout << "\n Tree Nodes : ";
    tree.preorder(tree.root);
    int x = 1;
    //Display calculated result
    cout << "\n Sum of parent node of [" << x << "] is : " << tree.parent_sum_of_x(tree.root, x) << "\n";
    return 0;
}

Output

 Tree Nodes :   6  7  1  10  1  1  3  1  1  5
 Sum of parent node of [1] is : 21
//Include namespace system
using System;
/*
	C# Program 
	Find the sum of the node having child node x
	Recursive solution
*/
//Binary Tree node
public 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;
	}
}
public class BinaryTree
{
	public Node root;
	public BinaryTree()
	{
		//Set initial tree root to null
		this.root = null;
	}
	//Display pre order elements
	public void preorder(Node node)
	{
		if (node != null)
		{
			//Print node value
			Console.Write("  " + node.data);
			preorder(node.left);
			preorder(node.right);
		}
	}
	// Returns the sum of all parent that child  contains x values
	public int parent_sum_of_x(Node node, int x)
	{
		int sum = 0;
		if (node != null)
		{
			if (node.left != null && node.left.data == x)
			{
				sum = node.data;
			}
			else if (node.right != null && node.right.data == x)
			{
				sum = node.data;
			}
			// Find the x node parent in left and right subtree
			sum = sum + parent_sum_of_x(node.left, x) + parent_sum_of_x(node.right, x);
		}
		return sum;
	}
	public static void Main(String[] args)
	{
		//Make object of binary tree
		BinaryTree tree = new BinaryTree();
		/*
			    constructor binary tree
			    -----------------------
			          6                            
			       /    \    
			      7      3     
			     / \      \               
			    1   10     1
			       /  \   / \
			      1    1 1   5
			  -----------------------
		*/
		tree.root = new Node(6);
		tree.root.left = new Node(7);
		tree.root.left.left = new Node(1);
		tree.root.left.right = new Node(10);
		tree.root.left.right.right = new Node(1);
		tree.root.left.right.left = new Node(1);
		tree.root.right = new Node(3);
		tree.root.right.right = new Node(1);
		tree.root.right.right.right = new Node(5);
		tree.root.right.right.left = new Node(1);
		Console.Write("\n Tree Nodes : ");
		tree.preorder(tree.root);
		int x = 1;
		//Display calculated result
		Console.Write("\n Sum of parent node of [" + x + "] is : " + tree.parent_sum_of_x(tree.root, x) + "\n");
	}
}

Output

 Tree Nodes :   6  7  1  10  1  1  3  1  1  5
 Sum of parent node of [1] is : 21
<?php
/*
	Php Program 
	Find the sum of the node having child node x
	Recursive solution
*/
//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;
	}
	//Display pre order elements
	public	function preorder($node)
	{
		if ($node != null)
		{
			//Print node value
			echo "  ". $node->data;
			$this->preorder($node->left);
			$this->preorder($node->right);
		}
	}
	// Returns the sum of all parent that child  contains x values
	public	function parent_sum_of_x($node, $x)
	{
		$sum = 0;
		if ($node != null)
		{
			if ($node->left != null && $node->left->data == $x)
			{
				$sum = $node->data;
			}
			else if ($node->right != null && $node->right->data == $x)
			{
				$sum = $node->data;
			}
			// Find the x node parent in left and right subtree
			$sum = $sum + $this->parent_sum_of_x($node->left, $x) + $this->parent_sum_of_x($node->right, $x);
		}
		return $sum;
	}
}

function main()
{
	//Make object of binary tree
	$tree = new BinaryTree();
	/*
		    constructor binary tree
		    -----------------------
		          6                            
		       /    \    
		      7      3     
		     / \      \               
		    1   10     1
		       /  \   / \
		      1    1 1   5
		  -----------------------
	*/
	$tree->root = new Node(6);
	$tree->root->left = new Node(7);
	$tree->root->left->left = new Node(1);
	$tree->root->left->right = new Node(10);
	$tree->root->left->right->right = new Node(1);
	$tree->root->left->right->left = new Node(1);
	$tree->root->right = new Node(3);
	$tree->root->right->right = new Node(1);
	$tree->root->right->right->right = new Node(5);
	$tree->root->right->right->left = new Node(1);
	echo "\n Tree Nodes : ";
	$tree->preorder($tree->root);
	$x = 1;
	//Display calculated result
	echo "\n Sum of parent node of [". $x ."] is : ". $tree->parent_sum_of_x($tree->root, $x) ."\n";
}
main();

Output

 Tree Nodes :   6  7  1  10  1  1  3  1  1  5
 Sum of parent node of [1] is : 21
/*
	Node Js Program 
	Find the sum of the node having child node x
	Recursive solution
*/
//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;
	}
	//Display pre order elements
	preorder(node)
	{
		if (node != null)
		{
			//Print node value
			process.stdout.write("  " + node.data);
			this.preorder(node.left);
			this.preorder(node.right);
		}
	}
	// Returns the sum of all parent that child  contains x values
	parent_sum_of_x(node, x)
	{
		var sum = 0;
		if (node != null)
		{
			if (node.left != null && node.left.data == x)
			{
				sum = node.data;
			}
			else if (node.right != null && node.right.data == x)
			{
				sum = node.data;
			}
			// Find the x node parent in left and right subtree
			sum = sum + this.parent_sum_of_x(node.left, x) + this.parent_sum_of_x(node.right, x);
		}
		return sum;
	}
}

function main()
{
	//Make object of binary tree
	var tree = new BinaryTree();
	/*
		    constructor binary tree
		    -----------------------
		          6                            
		       /    \    
		      7      3     
		     / \      \               
		    1   10     1
		       /  \   / \
		      1    1 1   5
		  -----------------------
	*/
	tree.root = new Node(6);
	tree.root.left = new Node(7);
	tree.root.left.left = new Node(1);
	tree.root.left.right = new Node(10);
	tree.root.left.right.right = new Node(1);
	tree.root.left.right.left = new Node(1);
	tree.root.right = new Node(3);
	tree.root.right.right = new Node(1);
	tree.root.right.right.right = new Node(5);
	tree.root.right.right.left = new Node(1);
	process.stdout.write("\n Tree Nodes : ");
	tree.preorder(tree.root);
	var x = 1;
	//Display calculated result
	process.stdout.write("\n Sum of parent node of [" + x + "] is : " + tree.parent_sum_of_x(tree.root, x) + "\n");
}
main();

Output

 Tree Nodes :   6  7  1  10  1  1  3  1  1  5
 Sum of parent node of [1] is : 21
#   Python 3 Program 
#   Find the sum of the node having child node x
#   Recursive solution

# 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
	
	# Display pre order elements
	def preorder(self, node) :
		if (node != None) :
			# Print node value
			print("  ", node.data, end = "")
			self.preorder(node.left)
			self.preorder(node.right)
		
	
	#  Returns the sum of all parent that child  contains x values
	def parent_sum_of_x(self, node, x) :
		sum = 0
		if (node != None) :
			if (node.left != None and node.left.data == x) :
				sum = node.data
			
			elif(node.right != None and node.right.data == x) :
				sum = node.data
			
			#  Find the x node parent in left and right subtree
			sum = sum + self.parent_sum_of_x(node.left, x) + self.parent_sum_of_x(node.right, x)
		
		return sum
	

def main() :
	# Make object of binary tree
	tree = BinaryTree()
	# 
	#             constructor binary tree
	#             -----------------------
	#                   6                            
	#                /    \    
	#               7      3     
	#              / \      \               
	#             1   10     1
	#                /  \   / \
	#               1    1 1   5
	#           -----------------------
	#         
	
	tree.root = Node(6)
	tree.root.left = Node(7)
	tree.root.left.left = Node(1)
	tree.root.left.right = Node(10)
	tree.root.left.right.right = Node(1)
	tree.root.left.right.left = Node(1)
	tree.root.right = Node(3)
	tree.root.right.right = Node(1)
	tree.root.right.right.right = Node(5)
	tree.root.right.right.left = Node(1)
	print("\n Tree Nodes : ", end = "")
	tree.preorder(tree.root)
	x = 1
	# Display calculated result
	print("\n Sum of parent node of [", x ,"] is : ", tree.parent_sum_of_x(tree.root, x) ,"\n", end = "")

if __name__ == "__main__": main()

Output

 Tree Nodes :    6   7   1   10   1   1   3   1   1   5
 Sum of parent node of [ 1 ] is :  21
#   Ruby Program 
#   Find the sum of the node having child node x
#   Recursive solution

# 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

	# Display pre order elements
	def preorder(node) 
		if (node != nil) 
			# Print node value
			print("  ", node.data)
			self.preorder(node.left)
			self.preorder(node.right)
		end

	end

	#  Returns the sum of all parent that child  contains x values
	def parent_sum_of_x(node, x) 
		sum = 0
		if (node != nil) 
			if (node.left != nil && node.left.data == x) 
				sum = node.data
			elsif(node.right != nil && node.right.data == x) 
				sum = node.data
			end

			#  Find the x node parent in left and right subtree
			sum = sum + self.parent_sum_of_x(node.left, x) + self.parent_sum_of_x(node.right, x)
		end

		return sum
	end

end

def main() 
	# Make object of binary tree
	tree = BinaryTree.new()
	# 
	#             constructor binary tree
	#             -----------------------
	#                   6                            
	#                /    \    
	#               7      3     
	#              / \      \               
	#             1   10     1
	#                /  \   / \
	#               1    1 1   5
	#           -----------------------
	#         
	
	tree.root = Node.new(6)
	tree.root.left = Node.new(7)
	tree.root.left.left = Node.new(1)
	tree.root.left.right = Node.new(10)
	tree.root.left.right.right = Node.new(1)
	tree.root.left.right.left = Node.new(1)
	tree.root.right = Node.new(3)
	tree.root.right.right = Node.new(1)
	tree.root.right.right.right = Node.new(5)
	tree.root.right.right.left = Node.new(1)
	print("\n Tree Nodes : ")
	tree.preorder(tree.root)
	x = 1
	# Display calculated result
	print("\n Sum of parent node of [", x ,"] is : ", tree.parent_sum_of_x(tree.root, x) ,"\n")
end

main()

Output

 Tree Nodes :   6  7  1  10  1  1  3  1  1  5
 Sum of parent node of [1] is : 21
/*
	Scala Program 
	Find the sum of the node having child node x
	Recursive solution
*/

//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);
	}
	//Display pre order elements
	def preorder(node: Node): Unit = {
		if (node != null)
		{
			//Print node value
			print("  " + node.data);
			preorder(node.left);
			preorder(node.right);
		}
	}
	// Returns the sum of all parent that child  contains x values
	def parent_sum_of_x(node: Node, x: Int): Int = {
		var sum: Int = 0;
		if (node != null)
		{
			if (node.left != null && node.left.data == x)
			{
				sum = node.data;
			}
			else if (node.right != null && node.right.data == x)
			{
				sum = node.data;
			}
			// Find the x node parent in left and right subtree
			sum = sum + parent_sum_of_x(node.left, x) + parent_sum_of_x(node.right, x);
		}
		return sum;
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		//Make object of binary tree
		var tree: BinaryTree = new BinaryTree();
		/*
			    constructor binary tree
			    -----------------------
			          6                            
			       /    \    
			      7      3     
			     / \      \               
			    1   10     1
			       /  \   / \
			      1    1 1   5
			  -----------------------
		*/
		tree.root = new Node(6);
		tree.root.left = new Node(7);
		tree.root.left.left = new Node(1);
		tree.root.left.right = new Node(10);
		tree.root.left.right.right = new Node(1);
		tree.root.left.right.left = new Node(1);
		tree.root.right = new Node(3);
		tree.root.right.right = new Node(1);
		tree.root.right.right.right = new Node(5);
		tree.root.right.right.left = new Node(1);
		print("\n Tree Nodes : ");
		tree.preorder(tree.root);
		var x: Int = 1;
		//Display calculated result
		print("\n Sum of parent node of [" + x + "] is : " + tree.parent_sum_of_x(tree.root, x) + "\n");
	}
}

Output

 Tree Nodes :   6  7  1  10  1  1  3  1  1  5
 Sum of parent node of [1] is : 21
/*
	Swift 4 Program 
	Find the sum of the node having child node x
	Recursive solution
*/

// 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;
	}
	// Display pre order elements
	func preorder(_ node: Node? )
	{
		if (node != nil)
		{
			//Print node value
			print("  ", node!.data, terminator: "");
			self.preorder(node!.left);
			self.preorder(node!.right);
		}
	}
	// Returns the sum of all parent that child  contains x values
	func parent_sum_of_x(_ node: Node? , _ x : Int)->Int
	{
		var sum: Int = 0;
		if (node != nil)
		{
			if (node!.left != nil && node!.left!.data == x)
			{
				sum = node!.data;
			}
			else if (node!.right != nil && node!.right!.data == x)
			{
				sum = node!.data;
			}
			// Find the x node parent in left and right subtree
			sum = sum + self.parent_sum_of_x(node!.left, x) + self.parent_sum_of_x(node!.right, x);
		}
		return sum;
	}
}
func main()
{
	//Make object of binary tree
	let tree: BinaryTree = BinaryTree();
	/*
	    constructor binary tree
	    -----------------------
	          6                            
	       /    \    
	      7      3     
	     / \      \               
	    1   10     1
	       /  \   / \
	      1    1 1   5
	  -----------------------
*/
	tree.root = Node(6);
	tree.root!.left = Node(7);
	tree.root!.left!.left = Node(1);
	tree.root!.left!.right = Node(10);
	tree.root!.left!.right!.right = Node(1);
	tree.root!.left!.right!.left = Node(1);
	tree.root!.right = Node(3);
	tree.root!.right!.right = Node(1);
	tree.root!.right!.right!.right = Node(5);
	tree.root!.right!.right!.left = Node(1);
	print("\n Tree Nodes : ", terminator: "");
	tree.preorder(tree.root);
	let x: Int = 1;
	//Display calculated result
	print("\n Sum of parent node of [", x ,"] is : ", tree.parent_sum_of_x(tree.root, x) ,"\n", terminator: "");
}
main();

Output

 Tree Nodes :    6   7   1   10   1   1   3   1   1   5
 Sum of parent node of [ 1 ] is :  21

Comment

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