in DS
1,851 views
3 votes
3 votes

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 _________________

in DS
by
1.9k views

4 Comments

2?
0
0
no....
0
0
its 3
0
0

5 Answers

0 votes
0 votes

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.