GATE CS 2023 | Question 47
Programming & Data Structures › Trees · MCQ · 2 marks · Answer: C
- Question 47Programming & Data Structuresmcq2 marks
foo(p)returnsp->val + foo(p->left) + foo(p->right)and prints that sum. For the binary tree shown (root 10; left subtree root 5 with children 3,8; right subtree root 11 with right child 13), what does foo print when called on the root?
- A.3 8 5 13 11 10
- B.3 5 8 10 11 13
- C.3 8 16 13 24 50✓
- D.3 16 8 50 24 13
Answer: C
Solution
Answer: (C) 3 8 16 13 24 50foocomputes subtree sums and prints each AFTER recursing (post-order), printing the subtree sum:
Output: 3 8 16 13 24 50.foo(3)=3 -> prints 3 foo(8)=8 -> prints 8 foo(5)=5+3+8=16 -> prints 16 foo(13)=13 -> prints 13 foo(11)=11+0+13=24 -> prints 24 foo(10)=10+16+24=50 -> prints 50 - A.
More GATE PYQ on Trees
All GATE CS Programming & Data Structures PYQ →Attempt GATE CS 2023 in real GATE exam mode
Full paper with a timer, the GATE interface and a topic-wise analysis — free.
Create free account →