C - Ternary operate to if else

2019-03-07 18:47发布

问题:

Can someone help me convert this line to an if-else statement?

temp = head->left ? head->left : head->right;

Thanks!

回答1:

if ( head->left )
    temp = head->left;
else
    temp = head->right;


回答2:

if (head->left) 
{
  temp = head->left;
}
else
{
  temp = head->right;
}