The problem I'm trying to solve is that given a binary tree, remove subtree which has the same value as the parameter value passed. Here's my code but I don't believe it works as the altered tree is the exact same as the original tree.
Before:
5
/ \
3 2
/ \ / \
2 1 4 3
After removal of subtree of value 2:
5
/
3
\
1
public TreeNode removeSubtree(TreeNode root, int value){
TreeNode copy = root;
removeSubtreeRecursion(copy, value);
return root;
}
public void removeSubtreeRecursion(TreeNode root, int val){
if(root == null) return;
else if(root.val == val) root = null;
else{
removeSubtreeRecursion(root.left, val);
removeSubtreeRecursion(root.right, val);
}
}