I'm new to Java (only been programming a month) and so I've been practicing some concepts. So if this is a naive or stupid question please be patient with me. I've only ever programmed in MATLAB before!
I just successfully inverted a binary tree (on my first try too!). However, I'm struggling with why my method is actually modifying the tree that I'm passing as input as opposed to leaving it intact and simply returning an inverted variant. In short, after the following line in my main function:
inverse = invertBinaryTree(root);
'inverse' and 'root' are both equal and they're the inverted version of 'root' BEFORE I passed 'root' into the method 'invertBinaryTree'. Please, I'm losing my mind trying to understand this behavior!
Matter of fact, I even tried creating a secondary method 'invertBinaryTree2' and this variant copies the input to a placeholder node and operates on the placeholder node instead of the input; however, this doesn't help things, my input still remains changed!
Here is my first entire main function with the supporting functions. Both invertBinaryTree and invertBinaryTree2 behave the same way, even though I'm copying root into a placeholder variable!
public class leetCode_InvertBinaryTree {
public static void main(String[] args) {
/*
* initializing some binaryNodes for the tree
*/
binaryNode a = new binaryNode(4);
binaryNode b = new binaryNode(2);
binaryNode c = new binaryNode(7);
binaryNode d = new binaryNode(1);
binaryNode e = new binaryNode(3);
binaryNode f = new binaryNode(6);
binaryNode g = new binaryNode(9);
binaryNode inverse;
/*
* just building the tree
*/
a.left = b;
a.right = c;
b.left = d;
b.right = e;
c.left = f;
c.right = g;
/*
* Here is the issue!
*/
System.out.println("Before inverting, root's left branch contains: " + a.left.data);
inverse = invertBinaryTree(a);
System.out.println("After inverting, root's left branch contains: " + a.left.data);
System.out.println("After inverting, inverse's left branch contains: " + inverse.left.data);
}
public static binaryNode invertBinaryTree(binaryNode root){
if(root.right == null && root.left == null){
return root;
}
else if(root.right == null){
root.right = root.left;
root.left = null;
}
else if (root.left == null){
root.left = root.right;
root.right = null;
}
else {
binaryNode temp = root.left;
root.left = root.right;
root.right = temp;
}
root.left = invertBinaryTree(root.left);
root.right = invertBinaryTree(root.right);
return root;
}
public static binaryNode invertBinaryTree2(binaryNode root){
binaryNode placeholder = root;
if(placeholder.right == null && placeholder.left == null){
return placeholder;
}
else if(placeholder.right == null){
placeholder.right = placeholder.left;
placeholder.left = null;
}
else if (placeholder.left == null){
placeholder.left = placeholder.right;
placeholder.right = null;
}
else {
binaryNode temp = placeholder.left;
placeholder.left = placeholder.right;
placeholder.right = temp;
}
placeholder.left = invertBinaryTree(placeholder.left);
placeholder.right = invertBinaryTree(placeholder.right);
return root;
}
Not that it matters, but here's the binaryNode class in case it helps you help me. I've also included the output of the main function that I have posted above.
Before inverting, root's left branch contains: 2
After inverting, root's left branch contains: 7
After inverting, inverse's left branch contains: 7
public class binaryNode {
public int data;
public binaryNode left;
public binaryNode right;
/*
* constructor for binaryNode
*/
public binaryNode(int d){
data = d;
}}