Data Structures & AlgorithmsModerateWipro · Accenture pattern
What is printed? (count leaves, internal nodes and full nodes of a binary tree)
What is printed? (count leaves, internal nodes and full nodes of a binary tree)
T = {1: (2, 3), 2: (4, None), 3: (5, 6), 4: (None, 7), 5: (None, None), 6: (None, None), 7: (None, None)}
leaves = sum(1 for v, (l, r) in T.items() if l is None and r is None)
full = sum(1 for v, (l, r) in T.items() if l and r)
print(leaves, len(T) - leaves, full)- A. 3 4 3
- B. 3 4 2
- C. 4 3 2
- D. 3 4 1
Correct answer: B. 3 4 2
Leaves 5, 6, 7; internal 1, 2, 3, 4; nodes with two children are 1 and 3. For any binary tree, leaves = full nodes + 1 (3 = 2 + 1), regardless of nodes with one child.
Practise more Trees, Graphs, Recursion and Dynamic Programming questionsTrees, Graphs, Recursion and Dynamic Programming: notes and key points
More Trees, Graphs, Recursion and Dynamic Programming questions
- A binary tree has preorder A B D E C F G and inorder D B E A F C G. What is its postorder? python def post(pre, ino): if not pre: return…
- How many structurally different binary trees have n = 4 nodes, and how many distinct labelled binary trees can be formed from 4 distinct…
- What is printed? (number of nodes in a complete binary tree in O(log² n)) python def count(nnodes): # implicit complete tree with nodes…
- What is printed? (AVL: which rotation is needed after inserting 30, 10, 20 into an empty AVL tree?) python def balancecase(order): a, b, c…
- What is the minimum number of nodes in an AVL tree of height h (height of a single node = 0)? python def nmin(h): a, b = 1, 2 # N(0), N(1)…
- BFS from vertex 0 visits neighbours in increasing order. What is the BFS order and the DFS (recursive) order? python g = {0: [1, 2], 1…