Sum of nodes having sum of subtrees of opposition parties
Here given code implementation process.
// C program for
// Sum of nodes having sum of subtrees of opposition parties
#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 *new_node = (struct TreeNode *) malloc(sizeof(struct TreeNode));
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;
}
// Calculate sum of all node which are left and right
// subtree have Even and Odd sum
int node_sum(struct TreeNode *node, int *sum)
{
if (node == NULL)
{
return 0;
}
// Find left and right subtree sum
int l = node_sum(node->left, sum);
int r = node_sum(node->right, sum);
if (node->left != NULL && node->right != NULL && (l + r) % 2 != 0)
{
// When node have both exist left and right subtree
// and its left and right subtree sum are even odd
*sum = *sum + node->data;
}
// returns the subtree sum
return l + r + node->data;
}
// Handles the request to find sum of opposite subtree
void opposite_sum(struct TreeNode *root)
{
if (root == NULL)
{
printf(" Empty Tree\n");
}
else
{
int sum = 0;
node_sum(root, & sum);
// Display calculate sum
printf(" Sum : %d\n", sum);
}
}
int main(int argc, char const *argv[])
{
struct BinaryTree *tree = new_tree();
/*
constructor binary tree
-----------------
6
/ \
4 7
/ \ \
2 3 12
/ \
10 8
\
-1
-----------------
First Tree
*/
tree->root = get_node(6);
tree->root->left = get_node(4);
tree->root->left->right = get_node(3);
tree->root->left->right->left = get_node(10);
tree->root->left->right->right = get_node(8);
tree->root->left->right->right->right = get_node(-1);
tree->root->left->left = get_node(2);
tree->root->right = get_node(7);
tree->root->right->right = get_node(12);
/*
Adding the left and right subtree value
-----------------
45
/ \
26 19
/ \ \
2 20 12
/ \
10 7
\
-1
-----------------
Second tree
Resultant nodes in First tree
—————————————————————————————
[3] Because left subtree sum 10 and right subtree sum have 7
[6] Because left subtree sum 26 and right subtree sum have 19
—————
[9] Output
*/
opposite_sum(tree->root);
return 0;
}
input
Sum : 9
/*
Java Program for
Sum of nodes having sum of subtrees of opposition parties
*/
// 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;
}
}
// Define Binary Tree
public class BinaryTree
{
public TreeNode root;
public int sum;
public BinaryTree()
{
this.root = null;
this.sum = 0;
}
// Calculate sum of all node which are left and right
// subtree have Even and Odd sum
public int nodeSum(TreeNode node)
{
if (node == null)
{
return 0;
}
// Find left and right subtree sum
int l = nodeSum(node.left);
int r = nodeSum(node.right);
if (node.left != null && node.right != null && (l + r) % 2 != 0)
{
// When node have both exist left and right subtree
// and its left and right subtree sum are even odd
this.sum = this.sum + node.data;
}
// returns the subtree sum
return l + r + node.data;
}
// Handles the request to find sum of opposite subtree
public void oppositeSum()
{
if (this.root == null)
{
System.out.println(" Empty Tree");
}
else
{
this.sum = 0;
this.nodeSum(this.root);
// Display calculate sum
System.out.println(" Sum : " + sum);
}
}
public static void main(String[] args)
{
BinaryTree tree = new BinaryTree();
/*
constructor binary tree
-----------------
6
/ \
4 7
/ \ \
2 3 12
/ \
10 8
\
-1
-----------------
First Tree
*/
tree.root = new TreeNode(6);
tree.root.left = new TreeNode(4);
tree.root.left.right = new TreeNode(3);
tree.root.left.right.left = new TreeNode(10);
tree.root.left.right.right = new TreeNode(8);
tree.root.left.right.right.right = new TreeNode(-1);
tree.root.left.left = new TreeNode(2);
tree.root.right = new TreeNode(7);
tree.root.right.right = new TreeNode(12);
/*
Adding the left and right subtree value
-----------------
45
/ \
26 19
/ \ \
2 20 12
/ \
10 7
\
-1
-----------------
Second tree
Resultant nodes in First tree
—————————————————————————————
[3] Because left subtree sum 10 and right subtree sum have 7
[6] Because left subtree sum 26 and right subtree sum have 19
—————
[9] Output
*/
tree.oppositeSum();
}
}
input
Sum : 9
// Include header file
#include <iostream>
using namespace std;
/*
C++ Program for
Sum of nodes having sum of subtrees of opposition parties
*/
// 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;
}
};
// Define Binary Tree
class BinaryTree
{
public:
TreeNode *root;
int sum;
BinaryTree()
{
this->root = NULL;
this->sum = 0;
}
// Calculate sum of all node which are left and right
// subtree have Even and Odd sum
int nodeSum(TreeNode *node)
{
if (node == NULL)
{
return 0;
}
// Find left and right subtree sum
int l = this->nodeSum(node->left);
int r = this->nodeSum(node->right);
if (node->left != NULL && node->right != NULL && (l + r) % 2 != 0)
{
// When node have both exist left and right subtree
// and its left and right subtree sum are even odd
this->sum = this->sum + node->data;
}
// returns the subtree sum
return l + r + node->data;
}
// Handles the request to find sum of opposite subtree
void oppositeSum()
{
if (this->root == NULL)
{
cout << " Empty Tree" << endl;
}
else
{
this->sum = 0;
this->nodeSum(this->root);
// Display calculate sum
cout << " Sum : " << this->sum << endl;
}
}
};
int main()
{
BinaryTree *tree = new BinaryTree();
/*
constructor binary tree
-----------------
6
/ \
4 7
/ \ \
2 3 12
/ \
10 8
\
-1
-----------------
First Tree
*/
tree->root = new TreeNode(6);
tree->root->left = new TreeNode(4);
tree->root->left->right = new TreeNode(3);
tree->root->left->right->left = new TreeNode(10);
tree->root->left->right->right = new TreeNode(8);
tree->root->left->right->right->right = new TreeNode(-1);
tree->root->left->left = new TreeNode(2);
tree->root->right = new TreeNode(7);
tree->root->right->right = new TreeNode(12);
/*
Adding the left and right subtree value
-----------------
45
/ \
26 19
/ \ \
2 20 12
/ \
10 7
\
-1
-----------------
Second tree
Resultant nodes in First tree
—————————————————————————————
[3] Because left subtree sum 10 and right subtree sum have 7
[6] Because left subtree sum 26 and right subtree sum have 19
—————
[9] Output
*/
tree->oppositeSum();
return 0;
}
input
Sum : 9
// Include namespace system
using System;
/*
Csharp Program for
Sum of nodes having sum of subtrees of opposition parties
*/
// 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;
}
}
// Define Binary Tree
public class BinaryTree
{
public TreeNode root;
public int sum;
public BinaryTree()
{
this.root = null;
this.sum = 0;
}
// Calculate sum of all node which are left and right
// subtree have Even and Odd sum
public int nodeSum(TreeNode node)
{
if (node == null)
{
return 0;
}
// Find left and right subtree sum
int l = this.nodeSum(node.left);
int r = this.nodeSum(node.right);
if (node.left != null && node.right != null && (l + r) % 2 != 0)
{
// When node have both exist left and right subtree
// and its left and right subtree sum are even odd
this.sum = this.sum + node.data;
}
// returns the subtree sum
return l + r + node.data;
}
// Handles the request to find sum of opposite subtree
public void oppositeSum()
{
if (this.root == null)
{
Console.WriteLine(" Empty Tree");
}
else
{
this.sum = 0;
this.nodeSum(this.root);
// Display calculate sum
Console.WriteLine(" Sum : " + this.sum);
}
}
public static void Main(String[] args)
{
BinaryTree tree = new BinaryTree();
/*
constructor binary tree
-----------------
6
/ \
4 7
/ \ \
2 3 12
/ \
10 8
\
-1
-----------------
First Tree
*/
tree.root = new TreeNode(6);
tree.root.left = new TreeNode(4);
tree.root.left.right = new TreeNode(3);
tree.root.left.right.left = new TreeNode(10);
tree.root.left.right.right = new TreeNode(8);
tree.root.left.right.right.right = new TreeNode(-1);
tree.root.left.left = new TreeNode(2);
tree.root.right = new TreeNode(7);
tree.root.right.right = new TreeNode(12);
/*
Adding the left and right subtree value
-----------------
45
/ \
26 19
/ \ \
2 20 12
/ \
10 7
\
-1
-----------------
Second tree
Resultant nodes in First tree
—————————————————————————————
[3] Because left subtree sum 10 and right subtree sum have 7
[6] Because left subtree sum 26 and right subtree sum have 19
—————
[9] Output
*/
tree.oppositeSum();
}
}
input
Sum : 9
<?php
/*
Php Program for
Sum of nodes having sum of subtrees of opposition parties
*/
// 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;
}
}
// Define Binary Tree
class BinaryTree
{
public $root;
public $sum;
public function __construct()
{
$this->root = NULL;
$this->sum = 0;
}
// Calculate sum of all node which are left and right
// subtree have Even and Odd sum
public function nodeSum($node)
{
if ($node == NULL)
{
return 0;
}
// Find left and right subtree sum
$l = $this->nodeSum($node->left);
$r = $this->nodeSum($node->right);
if ($node->left != NULL && $node->right != NULL && ($l + $r) % 2 != 0)
{
// When node have both exist left and right subtree
// and its left and right subtree sum are even odd
$this->sum = $this->sum + $node->data;
}
// returns the subtree sum
return $l + $r + $node->data;
}
// Handles the request to find sum of opposite subtree
public function oppositeSum()
{
if ($this->root == NULL)
{
echo " Empty Tree\n";
}
else
{
$this->sum = 0;
$this->nodeSum($this->root);
// Display calculate sum
echo " Sum : ".$this->sum."\n";
}
}
}
function main()
{
$tree = new BinaryTree();
/*
constructor binary tree
-----------------
6
/ \
4 7
/ \ \
2 3 12
/ \
10 8
\
-1
-----------------
First Tree
*/
$tree->root = new TreeNode(6);
$tree->root->left = new TreeNode(4);
$tree->root->left->right = new TreeNode(3);
$tree->root->left->right->left = new TreeNode(10);
$tree->root->left->right->right = new TreeNode(8);
$tree->root->left->right->right->right = new TreeNode(-1);
$tree->root->left->left = new TreeNode(2);
$tree->root->right = new TreeNode(7);
$tree->root->right->right = new TreeNode(12);
/*
Adding the left and right subtree value
-----------------
45
/ \
26 19
/ \ \
2 20 12
/ \
10 7
\
-1
-----------------
Second tree
Resultant nodes in First tree
—————————————————————————————
[3] Because left subtree sum 10 and right subtree sum have 7
[6] Because left subtree sum 26 and right subtree sum have 19
—————
[9] Output
*/
$tree->oppositeSum();
}
main();
input
Sum : 9
/*
Node JS Program for
Sum of nodes having sum of subtrees of opposition parties
*/
// Binary Tree node
class TreeNode
{
constructor(data)
{
// Set node value
this.data = data;
this.left = null;
this.right = null;
}
}
// Define Binary Tree
class BinaryTree
{
constructor()
{
this.root = null;
this.sum = 0;
}
// Calculate sum of all node which are left and right
// subtree have Even and Odd sum
nodeSum(node)
{
if (node == null)
{
return 0;
}
// Find left and right subtree sum
var l = this.nodeSum(node.left);
var r = this.nodeSum(node.right);
if (node.left != null && node.right != null && (l + r) % 2 != 0)
{
// When node have both exist left and right subtree
// and its left and right subtree sum are even odd
this.sum = this.sum + node.data;
}
// returns the subtree sum
return l + r + node.data;
}
// Handles the request to find sum of opposite subtree
oppositeSum()
{
if (this.root == null)
{
console.log(" Empty Tree");
}
else
{
this.sum = 0;
this.nodeSum(this.root);
// Display calculate sum
console.log(" Sum : " + this.sum);
}
}
}
function main()
{
var tree = new BinaryTree();
/*
constructor binary tree
-----------------
6
/ \
4 7
/ \ \
2 3 12
/ \
10 8
\
-1
-----------------
First Tree
*/
tree.root = new TreeNode(6);
tree.root.left = new TreeNode(4);
tree.root.left.right = new TreeNode(3);
tree.root.left.right.left = new TreeNode(10);
tree.root.left.right.right = new TreeNode(8);
tree.root.left.right.right.right = new TreeNode(-1);
tree.root.left.left = new TreeNode(2);
tree.root.right = new TreeNode(7);
tree.root.right.right = new TreeNode(12);
/*
Adding the left and right subtree value
-----------------
45
/ \
26 19
/ \ \
2 20 12
/ \
10 7
\
-1
-----------------
Second tree
Resultant nodes in First tree
—————————————————————————————
[3] Because left subtree sum 10 and right subtree sum have 7
[6] Because left subtree sum 26 and right subtree sum have 19
—————
[9] Output
*/
tree.oppositeSum();
}
main();
input
Sum : 9
# Python 3 Program for
# Sum of nodes having sum of subtrees of opposition parties
# Binary Tree node
class TreeNode :
def __init__(self, data) :
# Set node value
self.data = data
self.left = None
self.right = None
# Define Binary Tree
class BinaryTree :
def __init__(self) :
self.root = None
self.sum = 0
# Calculate sum of all node which are left and right
# subtree have Even and Odd sum
def nodeSum(self, node) :
if (node == None) :
return 0
l = self.nodeSum(node.left)
r = self.nodeSum(node.right)
if (node.left != None and node.right != None and(l + r) % 2 != 0) :
# When node have both exist left and right subtree
# and its left and right subtree sum are even odd
self.sum = self.sum + node.data
# returns the subtree sum
return l + r + node.data
# Handles the request to find sum of opposite subtree
def oppositeSum(self) :
if (self.root == None) :
print(" Empty Tree")
else :
self.sum = 0
self.nodeSum(self.root)
# Display calculate sum
print(" Sum : ", self.sum)
def main() :
tree = BinaryTree()
# constructor binary tree
# -----------------
# 6
# / \
# 4 7
# / \ \
# 2 3 12
# / \
# 10 8
# \
# -1
# -----------------
# First Tree
tree.root = TreeNode(6)
tree.root.left = TreeNode(4)
tree.root.left.right = TreeNode(3)
tree.root.left.right.left = TreeNode(10)
tree.root.left.right.right = TreeNode(8)
tree.root.left.right.right.right = TreeNode(-1)
tree.root.left.left = TreeNode(2)
tree.root.right = TreeNode(7)
tree.root.right.right = TreeNode(12)
# Adding the left and right subtree value
# -----------------
# 45
# / \
# 26 19
# / \ \
# 2 20 12
# / \
# 10 7
# \
# -1
# -----------------
# Second tree
# Resultant nodes in First tree
# —————————————————————————————
# [3] Because left subtree sum 10 and right subtree sum have 7
# [6] Because left subtree sum 26 and right subtree sum have 19
# —————
# [9] Output
tree.oppositeSum()
if __name__ == "__main__": main()
input
Sum : 9
# Ruby Program for
# Sum of nodes having sum of subtrees of opposition parties
# 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
# Define Binary Tree
class BinaryTree
# Define the accessor and reader of class BinaryTree
attr_reader :root, :sum
attr_accessor :root, :sum
def initialize()
self.root = nil
self.sum = 0
end
# Calculate sum of all node which are left and right
# subtree have Even and Odd sum
def nodeSum(node)
if (node == nil)
return 0
end
# Find left and right subtree sum
l = self.nodeSum(node.left)
r = self.nodeSum(node.right)
if (node.left != nil && node.right != nil && (l + r) % 2 != 0)
# When node have both exist left and right subtree
# and its left and right subtree sum are even odd
self.sum = self.sum + node.data
end
# returns the subtree sum
return l + r + node.data
end
# Handles the request to find sum of opposite subtree
def oppositeSum()
if (self.root == nil)
print(" Empty Tree", "\n")
else
self.sum = 0
self.nodeSum(self.root)
# Display calculate sum
print(" Sum : ", self.sum, "\n")
end
end
end
def main()
tree = BinaryTree.new()
# constructor binary tree
# -----------------
# 6
# / \
# 4 7
# / \ \
# 2 3 12
# / \
# 10 8
# \
# -1
# -----------------
# First Tree
tree.root = TreeNode.new(6)
tree.root.left = TreeNode.new(4)
tree.root.left.right = TreeNode.new(3)
tree.root.left.right.left = TreeNode.new(10)
tree.root.left.right.right = TreeNode.new(8)
tree.root.left.right.right.right = TreeNode.new(-1)
tree.root.left.left = TreeNode.new(2)
tree.root.right = TreeNode.new(7)
tree.root.right.right = TreeNode.new(12)
# Adding the left and right subtree value
# -----------------
# 45
# / \
# 26 19
# / \ \
# 2 20 12
# / \
# 10 7
# \
# -1
# -----------------
# Second tree
# Resultant nodes in First tree
# —————————————————————————————
# [3] Because left subtree sum 10 and right subtree sum have 7
# [6] Because left subtree sum 26 and right subtree sum have 19
# —————
# [9] Output
tree.oppositeSum()
end
main()
input
Sum : 9
/*
Scala Program for
Sum of nodes having sum of subtrees of opposition parties
*/
// 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);
}
}
// Define Binary Tree
class BinaryTree(var root: TreeNode , var sum: Int)
{
def this()
{
this(null, 0);
}
// Calculate sum of all node which are left and right
// subtree have Even and Odd sum
def nodeSum(node: TreeNode): Int = {
if (node == null)
{
return 0;
}
// Find left and right subtree sum
var l: Int = nodeSum(node.left);
var r: Int = nodeSum(node.right);
if (node.left != null && node.right != null && (l + r) % 2 != 0)
{
// When node have both exist left and right subtree
// and its left and right subtree sum are even odd
this.sum = this.sum + node.data;
}
// returns the subtree sum
return l + r + node.data;
}
// Handles the request to find sum of opposite subtree
def oppositeSum(): Unit = {
if (this.root == null)
{
println(" Empty Tree");
}
else
{
this.sum = 0;
this.nodeSum(this.root);
// Display calculate sum
println(" Sum : " + sum);
}
}
}
object Main
{
def main(args: Array[String]): Unit = {
var tree: BinaryTree = new BinaryTree();
/*
constructor binary tree
-----------------
6
/ \
4 7
/ \ \
2 3 12
/ \
10 8
\
-1
-----------------
First Tree
*/
tree.root = new TreeNode(6);
tree.root.left = new TreeNode(4);
tree.root.left.right = new TreeNode(3);
tree.root.left.right.left = new TreeNode(10);
tree.root.left.right.right = new TreeNode(8);
tree.root.left.right.right.right = new TreeNode(-1);
tree.root.left.left = new TreeNode(2);
tree.root.right = new TreeNode(7);
tree.root.right.right = new TreeNode(12);
/*
Adding the left and right subtree value
-----------------
45
/ \
26 19
/ \ \
2 20 12
/ \
10 7
\
-1
-----------------
Second tree
Resultant nodes in First tree
—————————————————————————————
[3] Because left subtree sum 10 and right subtree sum have 7
[6] Because left subtree sum 26 and right subtree sum have 19
—————
[9] Output
*/
tree.oppositeSum();
}
}
input
Sum : 9
/*
Swift 4 Program for
Sum of nodes having sum of subtrees of opposition parties
*/
// 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;
}
}
// Define Binary Tree
class BinaryTree
{
var root: TreeNode? ;
var sum: Int;
init()
{
self.root = nil;
self.sum = 0;
}
// Calculate sum of all node which are left and right
// subtree have Even and Odd sum
func nodeSum(_ node: TreeNode? )->Int
{
if (node == nil)
{
return 0;
}
// Find left and right subtree sum
let l: Int = self.nodeSum(node!.left);
let r: Int = self.nodeSum(node!.right);
if (node!.left != nil && node!.right != nil && (l + r) % 2 != 0)
{
// When node have both exist left and right subtree
// and its left and right subtree sum are even odd
self.sum = self.sum + node!.data;
}
// returns the subtree sum
return l + r + node!.data;
}
// Handles the request to find sum of opposite subtree
func oppositeSum()
{
if (self.root == nil)
{
print(" Empty Tree");
}
else
{
self.sum = 0;
let _ = self.nodeSum(self.root);
// Display calculate sum
print(" Sum : ", self.sum);
}
}
}
func main()
{
let tree: BinaryTree = BinaryTree();
/*
constructor binary tree
-----------------
6
/ \
4 7
/ \ \
2 3 12
/ \
10 8
\
-1
-----------------
First Tree
*/
tree.root = TreeNode(6);
tree.root!.left = TreeNode(4);
tree.root!.left!.right = TreeNode(3);
tree.root!.left!.right!.left = TreeNode(10);
tree.root!.left!.right!.right = TreeNode(8);
tree.root!.left!.right!.right!.right = TreeNode(-1);
tree.root!.left!.left = TreeNode(2);
tree.root!.right = TreeNode(7);
tree.root!.right!.right = TreeNode(12);
/*
Adding the left and right subtree value
-----------------
45
/ \
26 19
/ \ \
2 20 12
/ \
10 7
\
-1
-----------------
Second tree
Resultant nodes in First tree
—————————————————————————————
[3] Because left subtree sum 10 and right subtree sum have 7
[6] Because left subtree sum 26 and right subtree sum have 19
—————
[9] Output
*/
tree.oppositeSum();
}
main();
input
Sum : 9
/*
Kotlin Program for
Sum of nodes having sum of subtrees of opposition parties
*/
// 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;
}
}
// Define Binary Tree
class BinaryTree
{
var root: TreeNode ? ;
var sum: Int;
constructor()
{
this.root = null;
this.sum = 0;
}
// Calculate sum of all node which are left and right
// subtree have Even and Odd sum
fun nodeSum(node: TreeNode ? ): Int
{
if (node == null)
{
return 0;
}
// Find left and right subtree sum
val l: Int = this.nodeSum(node.left);
val r: Int = this.nodeSum(node.right);
if (node.left != null && node.right != null && (l + r) % 2 != 0)
{
// When node have both exist left and right subtree
// and its left and right subtree sum are even odd
this.sum = this.sum + node.data;
}
// returns the subtree sum
return l + r + node.data;
}
// Handles the request to find sum of opposite subtree
fun oppositeSum(): Unit
{
if (this.root == null)
{
println(" Empty Tree");
}
else
{
this.sum = 0;
this.nodeSum(this.root);
// Display calculate sum
println(" Sum : " + this.sum);
}
}
}
fun main(args: Array < String > ): Unit
{
val tree: BinaryTree = BinaryTree();
/*
constructor binary tree
-----------------
6
/ \
4 7
/ \ \
2 3 12
/ \
10 8
\
-1
-----------------
First Tree
*/
tree.root = TreeNode(6);
tree.root?.left = TreeNode(4);
tree.root?.left?.right = TreeNode(3);
tree.root?.left?.right?.left = TreeNode(10);
tree.root?.left?.right?.right = TreeNode(8);
tree.root?.left?.right?.right?.right = TreeNode(-1);
tree.root?.left?.left = TreeNode(2);
tree.root?.right = TreeNode(7);
tree.root?.right?.right = TreeNode(12);
/*
Adding the left and right subtree value
-----------------
45
/ \
26 19
/ \ \
2 20 12
/ \
10 7
\
-1
-----------------
Second tree
Resultant nodes in First tree
—————————————————————————————
[3] Because left subtree sum 10 and right subtree sum have 7
[6] Because left subtree sum 26 and right subtree sum have 19
—————
[9] Output
*/
tree.oppositeSum();
}
input
Sum : 9
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