56 56 votes Consider the following C program segment struct CellNode{ struct CellNode *leftChild int element; struct CellNode *rightChild; }; int Dosomething (struct CellNode *ptr) { int value = 0; if(ptr != NULL) { if (ptr -> leftChild != NULL) value = 1 + DoSomething (ptr -> leftChild); if (ptr -> rightChild != NULL) value = max(value, 1 + Dosomething (ptr -> rightChild)); } return(value); } The value returned by the function $\text{DoSomething}$ when a pointer to the root of a non-empty tree is passed as argument is The number of leaf nodes in the tree The number of nodes in the tree The number of internal nodes in the tree The height of the tree Data Structures gatecse-2004 data-structures binary-tree normal + – Kathleen 14.1k views answer comment Share Follow Print See all 5 Comments 5 5 Comments reply Show 2 previous comments Mitali gupta commented Dec 7, 2020 reply Follow flag Take this tree . Result for all options will be as follows: A- 4 B-7 C-3 D-2 Running given code we get 2 as answer . So option D is right 2 2 replyShare nayab.rasool commented Sep 7, 2024 reply Follow flag Apart from option D, the remaining options talks about counting leaf nodes,internal nodes or total nodes, i guess while counting these nodes we dont need to take maximum value so we can choose the option D pretty quickly (we take maximum height of left subtree and right subtree as height so option D). is this approach correct? 1 1 replyShare N_i_t_i_n commented Nov 13, 2025 reply Follow flag Simply take 1 node tree then Value retured is 0 , Must be D 0 0 replyShare Please log in or register to add a comment.
Best answer 59 59 votes Correct Option: D It calculates Height of tree. Easy way to get this answer . Draw a tree where all $4$ parameters are different. Get a Tree for which Height, No of Internal Nodes & No of Leafs are different & Trace out this algorithm. Akash Kanase answered Nov 22, 2015 • edited May 5, 2021 by soujanyareddy13 Akash Kanase comment Share Follow See 1 comment 1 1 comment reply Ayush Upadhyaya commented Dec 9, 2017 i edited by Puja Mishra Dec 24, 2017 reply Follow flag The below diagram may make the code easier to understand. 4 4 replyShare Please log in or register to add a comment.
7 7 votes It actually calculates the height of the tree How I did that: I drew a tree then just tried the algo on this tree and then I modified the tree wisely, then I tried the algo one more time Bhagirathi answered Sep 21, 2014 Bhagirathi comment Share Follow See all 5 Comments 5 5 Comments reply Show 2 previous comments Amcodes commented Sep 26, 2020 reply Follow flag LOL 0 0 replyShare biswajitbora commented Oct 24, 2023 reply Follow flag there should be a section called “legend answer” for answers like these :P 2 2 replyShare js__ commented Feb 1 reply Follow flag take just 1 node *_* 0 0 replyShare Please log in or register to add a comment.
1 1 vote Try to run code on this type of tree: 1.here node at the same level are sibling(are right subtree). The node below are child(left subtree). It is kind of representaion of n ary tree. Nitesh_Yadav answered Jan 7, 2022 Nitesh_Yadav comment Share Follow See 1 comment 1 1 comment reply Overflow04 commented Oct 6, 2022 reply Follow flag Your figure can be represented as You will get answer as D 0 0 replyShare Please log in or register to add a comment.