warpstar
warpstar

Reputation: 483

What does this line return?

int lf = ((t.left==null) = (t.right==null)) ? 1:0;

it returns 1 if the statement in the bigger parenthesis is true, but in the middle, whats the point of assigning right value to lefT?

Upvotes: 0

Views: 153

Answers (2)

Lee Louviere
Lee Louviere

Reputation: 5262

Normally you'd have an equal sign to assign. The return of the assigned is the same as the RHS of the expression.

You'd use an equal sign in a expression within an if to assign and check the result at the same time.

// return first and third items added if they exist.
if ((list = GetItems()).Length > 2) { return list[0] + list[2]; } 

Right here all you have is a compiler error because t.left==null evaluates to (true/false) and you can't assign to that.

Upvotes: 10

user520288
user520288

Reputation:

If both t.left and t.right are either null or not null at the same time, then lf is 1 otherwise it is 0.

Also you have a typo in there. The line should be

int lf = ((t.left==null) == (t.right==null)) ? 1:0;

Notice the == between the two null checks.

Upvotes: 1

Related Questions