Question 51
MediumA binary tree's left and right views are the first and last nodes seen at each level. Which traversal finds them easily?
- A.
Popular companies
Prepare company by company
Hiring pattern, syllabus, past questions and mocks for each company.
All companiesPractice by subject
Data Structures and Algorithms
A binary tree's left and right views are the first and last nodes seen at each level. Which traversal finds them easily?
Binary search
Postorder only
Inorder only
Level-order traversal
Sign up free to see the answer and a step-by-step explanation.
What is a balanced binary tree?
A tree with only left children
One where the heights of the two subtrees of every node differ by at most a small amount (e.g. 1)
One with the same number of nodes on each level
A tree with all leaves at depth 1
Sign up free to see the answer and a step-by-step explanation.
How many edges does a tree with 50 nodes have?
100
49
50
51
Sign up free to see the answer and a step-by-step explanation.
What is the minimum possible height (levels) of a binary tree with 100 nodes?
6
7
8
50
Sign up free to see the answer and a step-by-step explanation.
What is the output of this code?
class Node:
def __init__(self, v, l=None, r=None):
self.v, self.l, self.r = v, l, r
root = Node(1, Node(2, Node(4), Node(5)), Node(3))
from collections import deque
q, out = deque([root]), []
while q:
level = []
for _ in range(len(q)):
n = q.popleft()
level.append(n.v)
for c in (n.l, n.r):
if c:
q.append(c)
out.append(level)
print(out)
[[1, 2, 3, 4, 5]]
[[1], [2], [3], [4], [5]]
Sign up free to see the answer and a step-by-step explanation.
What is the output of this code?
class Node:
def __init__(self, v, l=None, r=None):
self.v, self.l, self.r = v, l, r
root = Node(1, Node(2, Node(4), Node(5)), Node(3))
def lca(n, a, b):
if not n or n.v in (a, b):
return n
l, r = lca(n.l, a, b), lca(n.r, a, b)
return n if l and r else l or r
print(lca(root, 4, 5).v)
4
2
Sign up free to see the answer and a step-by-step explanation.
What is the output of this code?
class Node:
def __init__(self, v, l=None, r=None):
self.v, self.l, self.r = v, l, r
root = Node(1, Node(2, Node(4), Node(5)), Node(3))
best = 0
def depth(n):
global best
if not n:
return 0
l, r = depth(n.l), depth(n.r)
best = max(best, l + r)
return 1 + max(l, r)
depth(root)
print(best)
2
5
Sign up free to see the answer and a step-by-step explanation.
How many structurally different binary trees can be formed with 3 nodes?
8
5
6
3
Sign up free to see the answer and a step-by-step explanation.
What is the time complexity of finding the height of a binary tree with n nodes?
O(n log n)
O(n)
O(log n)
O(1)
Sign up free to see the answer and a step-by-step explanation.
In an array representation of a complete binary tree (0-indexed), where are the children of the node at index i?
2i and 2i + 1
i + 1 and i + 2
2i + 1 and 2i + 2
i/2 and i/2 + 1
Sign up free to see the answer and a step-by-step explanation.
[[1], [2, 3], [4, 5]]
[[4, 5], [2, 3], [1]]
1
5
4
3