-5

How does this code's logic work?

boolean left = printLevel(root.left, level - 1);
boolean right = printLevel(root.right, level - 1);

return left || right;
Giorgi Tsiklauri
  • 9,715
  • 8
  • 45
  • 66

2 Answers2

1

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.

Giorgi Tsiklauri
  • 9,715
  • 8
  • 45
  • 66
0

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
Tahmid Ali
  • 805
  • 9
  • 29