Question 21
EasyWhat is an n-ary tree?
- A.
A tree in which each node can have up to n children
- B.
A tree with exactly n nodes
- C.
A tree of height n
- D.
A binary tree with n leaves
Sign up free to see the answer and a step-by-step explanation.
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 an n-ary tree?
A tree in which each node can have up to n children
A tree with exactly n nodes
A tree of height n
A binary tree with n leaves
Sign up free to see the answer and a step-by-step explanation.
What is a trie mainly used for?
Sorting numbers
Finding shortest paths
Balancing a binary search tree
Storing strings so that searches by prefix are fast
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 h(t):
return 0 if t is None else 1 + max(h(t.l), h(t.r))
print(h(N(1, N(2, N(4)), N(3))))
1
4
2
3
Sign up free to see the answer and a step-by-step explanation.
What is the lowest common ancestor (LCA) of two nodes in a tree?
The parent of the first node
The root, always
The node with the smallest value
The deepest node that is an ancestor of both
Sign up free to see the answer and a step-by-step explanation.
A binary tree has root 1. Node 1's children are 2 (left) and 3 (right), and node 2's children are 4 (left) and 5 (right). What is the lowest common ancestor of nodes 4 and 5?
1
3
4
2
Sign up free to see the answer and a step-by-step explanation.
Which traversal should be used to delete every node of a tree safely (children before their parent)?
Inorder
Level order
Preorder
Postorder
Sign up free to see the answer and a step-by-step explanation.
Which traversal is used to copy a tree, creating each parent before its children?
Preorder
Postorder
Inorder
Reverse level order
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 ino(n):
return [] if not n else ino(n.l) + [n.v] + ino(n.r)
print(*ino(root))
4 2 5 1 3
4 5 2 3 1
2 4 5 1 3
1 2 4 5 3
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 post(n):
return [] if not n else post(n.l) + post(n.r) + [n.v]
print(*post(root))
1 2 4 5 3
4 5 2 3 1
4 2 5 1 3
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 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 height(n):
return 0 if not n else 1 + max(height(n.l), height(n.r))
print(height(root))
4
2
5
3
Sign up free to see the answer and a step-by-step explanation.