Question 31
MediumWhat is the output of this code?
class Node:
def ():
.v, .l, .r = v, l, r
root = Node(, Node(, Node(), Node()), Node())
():
n:
n.l n.r:
leaves(n.l) + leaves(n.r)
(leaves(root))
Popular companies
Prepare company by company
Hiring pattern, syllabus, past questions and mocks for each company.
All companiesPractice by subject
Data Structures and Algorithms
What is the output of this code?
class Node:
def ():
.v, .l, .r = v, l, r
root = Node(, Node(, Node(), Node()), Node())
():
n:
n.l n.r:
leaves(n.l) + leaves(n.r)
(leaves(root))
4
2
3
5
Sign up free to see the answer and a step-by-step explanation.
What is a complete binary tree?
Every level is full except possibly the last, which is filled from the left
A tree with only one path
Every node has 0 or 2 children
All leaves are at the same depth and every internal node has 2 children
Sign up free to see the answer and a step-by-step explanation.
In a full binary tree (every node has 0 or 2 children) with n internal nodes, how many leaves are there?
2n
n
n + 1
n − 1
Sign up free to see the answer and a step-by-step explanation.
Which two traversals together uniquely determine a binary tree (with distinct values)?
Level order alone
Preorder and postorder
Inorder and preorder (or inorder and postorder)
Only inorder
Sign up free to see the answer and a step-by-step explanation.
What is the output of this code?
class N:
def __init__(self, v, l=None, r=None):
self.v, self.l, self.r = v, l, r
root = N(1, N(2, N(4), N(5)), N(3, N(6), N(7)))
def pre(n):
return [n.v] + pre(n.l) + pre(n.r) if n else []
def post(n):
return post(n.l) + post(n.r) + [n.v] if n else []
print(pre(root), post(root))
[1, 2, 4, 5, 3, 6, 7] [4, 5, 2, 6, 7, 3, 1]
[1, 2, 3, 4, 5, 6, 7] [7, 6, 5, 4, 3, 2, 1]
Sign up free to see the answer and a step-by-step explanation.
What is the output of this code?
class N:
def __init__(self, v, l=None, r=None):
self.v, self.l, self.r = v, l, r
root = N(1, N(2, N(4), N(5)), N(3, N(6), N(7)))
from collections import deque
q, levels = deque([root]), []
while q:
levels.append([n.v for n in q])
for _ in range(len(q)):
n = q.popleft()
for c in (n.l, n.r):
if c:
q.append(c)
print(levels)
[[1], [2], [3], [4, 5, 6, 7]]
[[1, 2, 3, 4, 5, 6, 7]]
Sign up free to see the answer and a step-by-step explanation.
What is the output of this code?
class N:
def __init__(self, v, l=None, r=None):
self.v, self.l, self.r = v, l, r
def height(n):
return 0 if n is None else 1 + max(height(n.l), height(n.r))
t = N(1, N(2, N(4, N(8))), N(3))
print(height(t))
3
4
Sign up free to see the answer and a step-by-step explanation.
What is the output of this code?
class N:
def __init__(self, v, l=None, r=None):
self.v, self.l, self.r = v, l, r
best = 0
def depth(n):
global best
if not n:
return 0
a, b = depth(n.l), depth(n.r)
best = max(best, a + b)
return 1 + max(a, b)
t = N(1, N(2, N(4), N(5, N(6))), N(3))
depth(t)
print(best)
3
5
Sign up free to see the answer and a step-by-step explanation.
What is the output of this code?
class N:
def __init__(self, v, l=None, r=None):
self.v, self.l, self.r = v, l, r
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
t = N(3, N(5, N(6), N(2, N(7), N(4))), N(1, N(0), N(8)))
print(lca(t, 7, 4).v, lca(t, 6, 4).v, lca(t, 5, 8).v)
2 5 3
7 5 1
Sign up free to see the answer and a step-by-step explanation.
How many nodes can a binary tree of height h have at most (height = number of levels)?
h²
2ʰ − 1
2h
2ʰ
Sign up free to see the answer and a step-by-step explanation.
[4, 2, 5, 1, 6, 3, 7] [4, 5, 2, 6, 7, 3, 1]
[1, 2, 4, 5, 3, 6, 7] [4, 2, 5, 6, 3, 7, 1]
[[1], [2, 3], [4, 5, 6, 7]]
[[4, 5, 6, 7], [2, 3], [1]]
5
2
6
4
5 5 3
2 3 3