How does this code's logic work?
boolean left = printLevel(root.left, level - 1);
boolean right = printLevel(root.right, level - 1);
return left || right;
How does this code's logic work?
boolean left = printLevel(root.left, level - 1);
boolean right = printLevel(root.right, level - 1);
return left || right;
It returns the boolean
result of your expression.
For example, return false || true;
would first evaluate "false || true
", and then it will return the result of that evaluation, which is true
, according to the truth table.
The method printLevel
returns result in boolean format: true
or false
based on the input provided.
The results of the method printLevel
execution is stored to the variables left
and right
available in the 1st and 2nd line of the sample code you provided.
Now, ||
is doing logical OR
operation in the 3rd line. Which works following way:
true || false --> true
true || true --> true
false || true --> true
false || false --> false