Consider the following function height, to which pointer to the root node of a binary tree shown below is passed
Note that max(a,b) defined by #define max(a,b) (a>b)?a:b.
int height(Node *root)
The output of the above code will be _________________
The output will be -1. The reason is the expansion of max(a, b). There is a problem as we need another set of Parenthesis like below ((a> b)?a: b)) In the absence of the brackets, the conditional operation?: has lower precedence than '+', so first the comparison between a and b (left height and right height) will be done and then + will be evaluated before?, leading to a different result than expected, and it can be verified that the program will output -1 for the given input.
64.3k questions
77.9k answers
244k comments
80.0k users