GATE Computer ScienceVery hardGATE CS (PYQ pattern)
In a binary tree given as (key, left, right) = (10, (-20, (8), (-6, (4), -)), (15, (-3), (12, (7), (-9)))), a 'path' is any sequence of connected nodes that goes along parent-child edges without repeating a node (it need not pass through the root). What is the maximum possible sum of the keys on a path?
- A. 51
- B. 40
- C. 37
- D. 44
Correct answer: D. 44
At every node take the best downward gains (ignoring negative ones) from the two children: the best bend occurs at 15 (left gain 0, right gain 12 + 7 = 19) combined with its parent 10, giving a path 7-12-15-10 of sum 44.
Practise more Programming and Data Structures (GATE) questionsProgramming and Data Structures (GATE): notes and key points
More Programming and Data Structures (GATE) questions
- An AVL tree is built by inserting 1, 2, ..., 15 in increasing order (the perfect tree with root 8). Then keys 1, 3, 5, 2, 4 are deleted in…
- Cuckoo hashing uses two tables of size 7: h1(k) = k mod 7 in T1 and h2(k) = floor(k / 7) mod 7 in T2. To insert x, place it in T1[h1(x)]…
- Keys 19, 36, 5, 21, 4, 26, 14, 17, 25, 12, 40, 33 are inserted into a chained hash table with 7 buckets, h(k) = k mod 7, each new key…
- A max-heap is built from [90, 80, 70, 60, 50, 40, 30, 20, 10, 15, 25, 35, 45, 55, 65] by bottom-up heapify. Then the key at index 14 is…
- Two stacks (values and operators) evaluate 12-(3+4(5-2))/53+8/(1+1) with all operators left-associative and integer division truncating…
- How many index pairs i < j satisfy a[i] + a[j] == 10 in the array {3, 8, 5, 2, 7, 4, 6, 1, 9, 5, 3, 7}?