In Java, I'm working on coding a boolean method that traverses through an entire binary tree for a specific value (e.g. integer value 1) and if all the nodes are that value, the method returns true.
So far, I have the following:
public static boolean everything1(IntBTNode root) {
if (root.data == 1) {
everything1(root.left);
return true;
everything1(root.right);
}
}
Am I on the right track?
First of all, every line of code after a
returnstatement will never be executed. So careful there. Second, if you are going to do it with recursion, I recommend the next:true.&&because if you getfalsethe execution finish there, and you want to check if all the items are1