so i have to create a code that can sum two different binary trees and return the result of it in a third binary tree. It has to work using recursivity. Here's the code that i currently have. It kinda works but it does not working recursively.
public BTree btNodeSum(BTree b, BTree b2) throws TreeException {
if (isEmpty()) {
throw new TreeException("Binary Tree is empty");
}
BTree tree = new BTree();
tree.root = btNodeSum(b.root, b2.root);
return tree;
}
private BTreeNode btNodeSum(BTreeNode b, BTreeNode b2) throws TreeException{
BTreeNode n = new BTreeNode(0);
if(b == null)
return b2;
if(b2 == null)
return b;
if(b.left != null && b2.left != null){
b = btNodeSum(b.left, null);
b2 = btNodeSum(null, b2.left);
n.data = (int) btNodeSum(b, null).data + (int) btNodeSum(null, b2).data;
}
else if(b.right != null && b2.right != null){
b = btNodeSum(b.right, null);
b2 = btNodeSum(null, b2.right);
n.data = (int) btNodeSum(b, null).data + (int) btNodeSum(null, b2).data;
}
b = btNodeSum(b.left, null);
b2 = btNodeSum(null, b2.left);
b = btNodeSum(b.right, null);
b2 = btNodeSum(null, b2.right);
return n;
}
The two trees that i am trying to sum have these values:

And the output that a im getting is this one: sum of tree 1 and tree 2: PreOrder Tour: 8(null, 0),
I would appreciate all the help!!