GATE Exam  >  GATE Questions  >   Consider the following function, to which a ... Start Learning for Free
Consider the following function, to which a pointer to the root node of the binary tree T is passed.
int Gradeup (Struct Node * root)
{
int a = 0, b = 0, c = 0;
if(root == NULL) return 0;
if(root → left == NULL && root → right == NULL) return 1;
a = Gradeup (root → left);
b = Gradeup (root → right);
c = 1 + max(a, b);
return c;
}
The above program computes,
  • a)
    The number of edges in the longest path from root to leaf in T
  • b)
    The number of nodes in the longest path from root to leaf in T
  • c)
    Number of leaves in T
  • d)
    Total number of nodes in T
Correct answer is option 'B'. Can you explain this answer?
Verified Answer
Consider the following function, to which a pointer to the root node ...
The above program computes the number of levels in T, Option (B) is actually the definition of the number of levels (or depth) of a tree. For example, if the tree T has only one node, it returns 1. So options (A) and (C) are eliminated, as if (A) was supposed to be true, it should have returned 0. Now the only dilemma is between (B) and (D), which can also be handled easily, as the line C = 1 + max (a, B. should have been c = 1 + a + b instead, but that's not the case and therefore it only computes the number of levels in T. All this explanation is given to help you solve the question in the exam quickly, but with a lot of practice, just by looking at the code you will be able to tell what the function can do and cannot do.
View all questions of this test
Most Upvoted Answer
Consider the following function, to which a pointer to the root node ...
Understanding the Function
The function `Gradeup` computes a specific property of the binary tree passed to it via the pointer `root`. Let's break down its functionality:
Base Case
- If `root` is NULL, it returns 0, indicating there is no tree.
- If both left and right children of the root are NULL, it means the current node is a leaf, and it returns 1 (counting the leaf itself).
Recursive Calls
- The function recursively calculates:
- `a`: The longest path from the left child to a leaf.
- `b`: The longest path from the right child to a leaf.
Calculating the Longest Path
- The function then calculates `c` as `1 + max(a, b)`, which represents:
- The longest path from the current node to a leaf, where:
- `1` accounts for the current node,
- `max(a, b)` gives the length of the longest path from either the left or right child.
Final Output
- The function returns `c`, which effectively counts the number of nodes in the longest path from the root to a leaf.
Key Insight
- Since the function counts nodes (including the root node itself) in the longest path, the correct interpretation of the output is that it represents the number of nodes in that path, not the number of edges.
Conclusion
Thus, the correct answer is option 'B': The number of nodes in the longest path from root to leaf in T.
Explore Courses for GATE exam

Similar GATE Doubts

Consider the following function, to which a pointer to the root node of the binary tree T is passed.int Gradeup (Struct Node * root){int a = 0, b = 0, c = 0;if(root == NULL) return 0;if(root → left == NULL && root → right == NULL) return 1;a = Gradeup (root → left);b = Gradeup (root → right);c = 1 + max(a, b);return c;}The above program computes,a)The number of edges in the longest path from root to leaf in Tb)The number of nodes in the longest path from root to leaf in Tc)Number of leaves in Td)Total number of nodes in TCorrect answer is option 'B'. Can you explain this answer?
Question Description
Consider the following function, to which a pointer to the root node of the binary tree T is passed.int Gradeup (Struct Node * root){int a = 0, b = 0, c = 0;if(root == NULL) return 0;if(root → left == NULL && root → right == NULL) return 1;a = Gradeup (root → left);b = Gradeup (root → right);c = 1 + max(a, b);return c;}The above program computes,a)The number of edges in the longest path from root to leaf in Tb)The number of nodes in the longest path from root to leaf in Tc)Number of leaves in Td)Total number of nodes in TCorrect answer is option 'B'. Can you explain this answer? for GATE 2024 is part of GATE preparation. The Question and answers have been prepared according to the GATE exam syllabus. Information about Consider the following function, to which a pointer to the root node of the binary tree T is passed.int Gradeup (Struct Node * root){int a = 0, b = 0, c = 0;if(root == NULL) return 0;if(root → left == NULL && root → right == NULL) return 1;a = Gradeup (root → left);b = Gradeup (root → right);c = 1 + max(a, b);return c;}The above program computes,a)The number of edges in the longest path from root to leaf in Tb)The number of nodes in the longest path from root to leaf in Tc)Number of leaves in Td)Total number of nodes in TCorrect answer is option 'B'. Can you explain this answer? covers all topics & solutions for GATE 2024 Exam. Find important definitions, questions, meanings, examples, exercises and tests below for Consider the following function, to which a pointer to the root node of the binary tree T is passed.int Gradeup (Struct Node * root){int a = 0, b = 0, c = 0;if(root == NULL) return 0;if(root → left == NULL && root → right == NULL) return 1;a = Gradeup (root → left);b = Gradeup (root → right);c = 1 + max(a, b);return c;}The above program computes,a)The number of edges in the longest path from root to leaf in Tb)The number of nodes in the longest path from root to leaf in Tc)Number of leaves in Td)Total number of nodes in TCorrect answer is option 'B'. Can you explain this answer?.
Solutions for Consider the following function, to which a pointer to the root node of the binary tree T is passed.int Gradeup (Struct Node * root){int a = 0, b = 0, c = 0;if(root == NULL) return 0;if(root → left == NULL && root → right == NULL) return 1;a = Gradeup (root → left);b = Gradeup (root → right);c = 1 + max(a, b);return c;}The above program computes,a)The number of edges in the longest path from root to leaf in Tb)The number of nodes in the longest path from root to leaf in Tc)Number of leaves in Td)Total number of nodes in TCorrect answer is option 'B'. Can you explain this answer? in English & in Hindi are available as part of our courses for GATE. Download more important topics, notes, lectures and mock test series for GATE Exam by signing up for free.
Here you can find the meaning of Consider the following function, to which a pointer to the root node of the binary tree T is passed.int Gradeup (Struct Node * root){int a = 0, b = 0, c = 0;if(root == NULL) return 0;if(root → left == NULL && root → right == NULL) return 1;a = Gradeup (root → left);b = Gradeup (root → right);c = 1 + max(a, b);return c;}The above program computes,a)The number of edges in the longest path from root to leaf in Tb)The number of nodes in the longest path from root to leaf in Tc)Number of leaves in Td)Total number of nodes in TCorrect answer is option 'B'. Can you explain this answer? defined & explained in the simplest way possible. Besides giving the explanation of Consider the following function, to which a pointer to the root node of the binary tree T is passed.int Gradeup (Struct Node * root){int a = 0, b = 0, c = 0;if(root == NULL) return 0;if(root → left == NULL && root → right == NULL) return 1;a = Gradeup (root → left);b = Gradeup (root → right);c = 1 + max(a, b);return c;}The above program computes,a)The number of edges in the longest path from root to leaf in Tb)The number of nodes in the longest path from root to leaf in Tc)Number of leaves in Td)Total number of nodes in TCorrect answer is option 'B'. Can you explain this answer?, a detailed solution for Consider the following function, to which a pointer to the root node of the binary tree T is passed.int Gradeup (Struct Node * root){int a = 0, b = 0, c = 0;if(root == NULL) return 0;if(root → left == NULL && root → right == NULL) return 1;a = Gradeup (root → left);b = Gradeup (root → right);c = 1 + max(a, b);return c;}The above program computes,a)The number of edges in the longest path from root to leaf in Tb)The number of nodes in the longest path from root to leaf in Tc)Number of leaves in Td)Total number of nodes in TCorrect answer is option 'B'. Can you explain this answer? has been provided alongside types of Consider the following function, to which a pointer to the root node of the binary tree T is passed.int Gradeup (Struct Node * root){int a = 0, b = 0, c = 0;if(root == NULL) return 0;if(root → left == NULL && root → right == NULL) return 1;a = Gradeup (root → left);b = Gradeup (root → right);c = 1 + max(a, b);return c;}The above program computes,a)The number of edges in the longest path from root to leaf in Tb)The number of nodes in the longest path from root to leaf in Tc)Number of leaves in Td)Total number of nodes in TCorrect answer is option 'B'. Can you explain this answer? theory, EduRev gives you an ample number of questions to practice Consider the following function, to which a pointer to the root node of the binary tree T is passed.int Gradeup (Struct Node * root){int a = 0, b = 0, c = 0;if(root == NULL) return 0;if(root → left == NULL && root → right == NULL) return 1;a = Gradeup (root → left);b = Gradeup (root → right);c = 1 + max(a, b);return c;}The above program computes,a)The number of edges in the longest path from root to leaf in Tb)The number of nodes in the longest path from root to leaf in Tc)Number of leaves in Td)Total number of nodes in TCorrect answer is option 'B'. Can you explain this answer? tests, examples and also practice GATE tests.
Explore Courses for GATE exam
Signup for Free!
Signup to see your scores go up within 7 days! Learn & Practice with 1000+ FREE Notes, Videos & Tests.
10M+ students study on EduRev