Professional Documents
Culture Documents
Example One
Output:
[
[1, 2, 4],
[1, 2, 5],
[1, 3, 6],
[1, 3, 7]
]
There are four leafs in the given graph, so we have four paths: from the root to every leaf. Each
path is a list of the values from the tree nodes on the path, and we have four lists. They can go in
any order.
Example Two
Output:
[
[10, 20, 40],
[10, 20, 50],
[10, 30]
]
There are 3 paths in the tree.
The order of the paths (order of the lists in the outer list) does not matter, so [[10, 30], [10, 20,
40], [10, 20, 50]] is another correct answer.
Notes
Return a list of integer lists, where each list is representing a path.
The order of the paths (order of the lists in the outer list) does not matter.
Constraints:
"""
class BinaryTreeNode:
self.value = value
self.left = None
self.right = None
"""
def all_paths_of_a_binary_tree(root):
"""
Args:
root(BinaryTreeNode_int32)
Returns:
list_list_int32
"""
return []
Example
Output:
Notes
Constraints:
"""
class BinaryTreeNode:
self.value = value
self.left = None
self.right = None
"""
def postorder_traversal(root):
"""
Args:
root(BinaryTreeNode_int32)
Returns:
list_int32
"""
return []
Problem
The LCA of nodes a and b in a tree is defined as the shared ancestor node of a and b that is
located farthest from the root of the tree.
Example
a = 8, b = 9
Output:
5
There are three shared parents of 8 and 9 in this tree: 5, 2, 1. Of those three, the farthest from the
root is 5.
Other examples:
LCA(2, 5) = 2
LCA(2, 3) = 1
Notes
Constraints:
"""
class BinaryTreeNode:
self.value = value
self.left = None
self.right = None
"""
"""
Args:
root(BinaryTreeNode_int32)
a(BinaryTreeNode_int32)
b(BinaryTreeNode_int32)
Returns:
int32
"""
return 0
Problem
Given the root node of a tree of positive numbers, and a sequence of operations on the iterator,
calculate return values of all those operations.
Iterator has two operations:
1. has_next() should return 1 if one or more elements remain in the in-order traversal of the
tree, otherwise it should return 0.
2. next() should return the next value in the in-order traversal of the tree if it exists,
otherwise a special value of 0.
Execute operations one by one and return all their return values in a list.
Both operations must take constant time on average and use O(height of the tree) of extra
memory.
Example
Notes
It is a good idea to implement the iterator as a class (or object or struct - depending on
the language you use). In function implement_tree_iterator you would then create an
instance of that class/object/struct and call its methods/functions to execute the
operations.
Structure of the class may look like this:
class TreeIterator {
TreeIterator() {
// This is a constructor.
// Initialize required data structures.
}
int next() {
// ...
}
int has_next() {
// Or you can return bool here and convert it
// to int in function implement_tree_iterator.
}
}
Constraints:
"""
class BinaryTreeNode:
self.value = value
self.left = None
self.right = None
"""
Args:
root(BinaryTreeNode_int32)
operations(list_str)
Returns:
list_int32
"""
return []
Problem
Example
0
/ \
1 2
/ \ / \
3 4 5 6
Output:
0
/ \
2 1
/ \ / \
6 5 4 3
Notes
The function doesn't need to return anything. Modify the tree in-place.
Constraints:
class BinaryTreeNode:
self.value = value
self.left = None
self.right = None
"""
def mirror_image(root):
"""
Args:
root(BinaryTreeNode_int32)
Returns:
Nothing
"""
Problem
Example
Output:
Notes
Constraints:
class BinaryTreeNode:
self.value = value
self.left = None
self.right = None
"""
def clone_tree(root):
"""
Args:
root(BinaryTreeNode_int32)
Returns:
BinaryTreeNode_int32
"""
return None
Problem
Example
Output:
There is nothing to the right of the root node on the first level, so root.next_right == null.
First node of the seconds level points to the second node on the second level. Second node of
the second level has next_right == null.
And so on.
Notes
Constraints:
You might need this for debugging your solution on IK UpLevel platform.
Input file contains the given tree in the usual binary tree format, there is nothing
about next_right in the input file.
Output file, if the solution is correct, contains the same tree but includes the information stored
in the next_right pointers. As in the input, the tree is represented by a JSON array. null values
from input remain null values in the output. Non-null values (numbers) represent actual nodes of
the tree in the input. In the output, a node instead is represented by a JSON arrays with two
elements. For example, if node value was 5 in the input, then in the output it should either be [5,
null] if node.next_right == null or [5, 6] if node.next_right.value == 6.
Example output
is represented by
[[100, null],
[200, 300], [300, null],
[400, 500], [500, 600], [600, 700], [700, null]]
"""
class BinaryTreeNode:
self.value = value
self.left = None
self.right = None
self.next_right = None
"""
def populate_sibling_pointers(root):
"""
Args:
root(BinaryTreeNode_int32)
Returns:
BinaryTreeNode_int32
"""
Problem
Example One
Output:
[
[0],
[1],
[2],
[4],
[3]
]
Example Two
Output:
[
[2],
[5, 4],
[0, 1, 3, 6]
]
Notes
Constraints:
"""
class BinaryTreeNode:
self.value = value
self.left = None
self.right = None
"""
def level_order_traversal(root):
"""
Args:
root(BinaryTreeNode_int32)
Returns:
list_list_int32
"""
return []
Problem
Example One
Output:
[
[1],
[3, 4, 2],
[5, 6]
]
Example Two
Output:
[
[1],
[2],
[4],
[3]
]
Notes
Constraints:
"""
class TreeNode:
self.value = value
self.children = []
"""
def level_order(root):
"""
Args:
root(TreeNode_int32)
Returns:
list_list_int32
"""
return []
Problem
Example One
Output:
[
[3, 4],
[1, 2],
[0]
]
Example Two
Output:
[
[3],
[2],
[1],
[0]
]
Notes
Constraints:
"""
For your reference:
class BinaryTreeNode:
self.value = value
self.left = None
self.right = None
"""
def reverse_level_order_traversal(root):
"""
Args:
root(BinaryTreeNode_int32)
Returns:
list_list_int32
"""
return []
Example One
Output:
[0, 2, 4]
From the right side, the tree will look like below:
Example Two
Output:
[0, 1, 2, 3]
Notes
Constraints:
"""
class BinaryTreeNode:
self.value = value
self.left = None
self.right = None
"""
def right_view(root):
"""
Args:
root(BinaryTreeNode_int32)
Returns:
list_int32
"""
return []
Problem
Example One
Output:
[
[0],
[2, 1],
[3, 4]
]
Example Two
Output:
[
[0],
[1],
[2],
[3]
]
Notes
Constraints:
class BinaryTreeNode:
self.value = value
self.left = None
self.right = None
"""
def zigzag_level_order_traversal(root):
"""
Args:
root(BinaryTreeNode_int32)
Returns:
list_list_int32
"""
return []
Problem
Example One
k = 4
Output:
1
Path 0 -> 1 -> 3 has the sum of node values equal to 4.
Example Two
k = 10
Output:
Notes
Constraints:
class BinaryTreeNode:
self.value = value
self.left = None
self.right = None
"""
"""
Args:
root(BinaryTreeNode_int32)
k(int32)
Returns:
bool
"""
return False
Problem
Is It A BST
Given a binary tree, check if it is a binary search tree (BST). A valid BST does not have to be
complete or balanced.
1. All nodes values of left subtree are less than or equal to parent node value.
2. All nodes values of right subtree are greater than or equal to parent node value.
3. Both left subtree and right subtree must be BSTs.
4. NULL tree is a BST.
5. Single node trees (including leaf nodes of any tree) are BSTs.
Example One
Output:
0
Left child value 200 is greater than the parent node value 100; violates the definition of BST.
Example Two
Output:
Notes
Constraints:
class BinaryTreeNode:
self.value = value
self.left = None
self.right = None
"""
def is_bst(root):
"""
Args:
root(BinaryTreeNode_int32)
Returns:
bool
"""
return False
Problem
Example One
Output:
6
The input tree has a total of 6 nodes. Each node is a root of a subtree. All those 6 subtrees are
unival trees.
Example Two
Output:
5
The input tree has a total of 7 nodes, so there are 7 subtrees. Of those 7, all but two subtrees are
unival. The two non-unival subtrees are:
Constraints:
"""
class BinaryTreeNode:
self.value = value
self.left = None
self.right = None
"""
def find_single_value_trees(root):
"""
Args:
root(BinaryTreeNode_int32)
Returns:
int32
"""
return 0
Problem
Upside Down
Given a binary tree where every node has either 0 or 2 children and every right node is a leaf
node, flip it upside down turning it into a binary tree where all left nodes are leafs.
Example One
Output:
Example Two
Output:
The same output tree oriented differently:
Notes
Constraints:
"""
class BinaryTreeNode:
self.value = value
self.left = None
self.right = None
"""
def flip_upside_down(root):
"""
Args:
root(BinaryTreeNode_int32)
Returns:
BinaryTreeNode_int32
"""
return None
Problem
Example One
Output:
Example Two
Output:
Notes
A node with value equal to the value of the root node can be inserted either in the left or
right subtree.
A binary tree is called height-balanced if for each node the following property is satisfied:
o The difference in the heights of its left and right subtrees differ by at most 1.
Constraints:
class BinaryTreeNode:
self.value = value
self.left = None
self.right = None
"""
"""
Args:
root1(BinaryTreeNode_int32)
root2(BinaryTreeNode_int32)
Returns:
BinaryTreeNode_int32
"""
return None
Problem
Largest BST
Given a binary tree, find the largest subtree that's a binary search tree (BST).
Here the largest subtree means a subtree with maximum number of nodes.
Example
Output:
3
There are seven distinct subtrees. Five of them are BSTs (rooted in 300, 200, 400, 600, 700). Sizes
if those five are 3, 1, 1, 1 and 1 respectively. The largest BST subtree has 3 nodes.
Notes
There is only one argument named root denoting the root of the input tree.
Return an integer denoting the size of the largest BST.
Constraints:
"""
class BinaryTreeNode:
self.value = value
self.left = None
self.right = None
"""
def find_largest_bst(root):
"""
Args:
root(BinaryTreeNode_int32)
Returns:
int32
"""
return 0
Problem
Returned list should follow the in-order traversal order of the given tree.
The "root" node that you return should be the first node in the in-order traversal order. That
"root" node should be connected with the last node in the in-order traversal as if "root" node
goes after the last node and last node goes before the "root" node.
Example
Output:
Notes
Constraints:
You might need this for debugging your solution on IK UpLevel platform.
Input file contains the given tree in the usual binary tree format.
Example output
is represented by
[1, 2, 3, 4, 5, 4, 3, 2, 1]
If the returned data structure is not circular or otherwise incorrect, the output may contain the
correct portion of it, and you will find an error message in the ERROR field.
"""
class BinaryTreeNode:
self.value = value
self.left = None
self.right = None
"""
def binary_tree_to_cdll(root):
"""
Args:
root(BinaryTreeNode_int32)
Returns:
BinaryTreeNode_int32
"""
return None
Problem
Example One
{
"inorder": [2, 1, 3],
"preorder": [1, 2, 3]
}
Output:
1
/ \
2 3
Example Two
{
"inorder": [3, 2, 1, 5, 4, 6],
"preorder": [1, 2, 3, 4, 5, 6]
}
Output:
1
/ \
2 4
/ / \
3 5 6
Notes
Constraints:
"""
class BinaryTreeNode:
self.value = value
self.left = None
self.right = None
"""
"""
Args:
inorder(list_int32)
preorder(list_int32)
Returns:
BinaryTreeNode_int32
"""
return None
Problem
Example One
Output:
[0, 1, 3, 4, 2]
Example Two
Output:
[0, 1, 2, 3]
Notes
The preorder traversal processes all the nodes of a binary tree by first visiting the root, then
recursively visiting its left and right subtrees respectively.
Constraints:
"""
class BinaryTreeNode:
self.value = value
self.left = None
self.right = None
"""
def preorder(root):
"""
Args:
root(BinaryTreeNode_int32)
Returns:
list_int32
"""
return []
Problem
Example One
Output:
[3, 1, 4, 0, 2]
Example Two
Output:
[1, 3, 2, 0]
Notes
The inorder traversal of a binary tree first visits the left subtree, then the root and finally the right
subtree.
Constraints:
"""
class BinaryTreeNode:
self.left = None
self.right = None
"""
def inorder(root):
"""
Args:
root(BinaryTreeNode_int32)
Returns:
list_int32
"""
return []
Problem
Example One
Output:
[3, 1, 4, 0, 2]
Example Two
Output:
[1, 3, 2, 0]
Notes
The inorder traversal of a binary tree first visits the left subtree, then the root and finally the right
subtree.
Constraints:
"""
For your reference:
class BinaryTreeNode:
self.value = value
self.left = None
self.right = None
"""
def inorder(root):
"""
Args:
root(BinaryTreeNode_int32)
Returns:
list_int32
"""
return []
Problem
Example One
Output:
[3, 4, 1, 2, 0]
Example Two
Output:
[3, 2, 1, 0]
Notes
The postorder traversal visits all the nodes of a binary tree by recursively visiting the left subtree,
then the right subtree and finally visiting the root.
Constraints:
"""
class BinaryTreeNode:
self.value = value
self.left = None
self.right = None
"""
def postorder(root):
"""
Args:
root(BinaryTreeNode_int32)
Returns:
list_int32
"""
return []
Problem
Print All Paths That Sum To K
Given a binary tree and an integer k, find all the root to leaf paths that sum to k.
Example One
k = 80
Output:
[
[10, 25, 45]
[10, 30, 40]
]
Example Two
k = 10
Output:
[
[5, 5],
[5, 5]
]
Notes
In case there is no root to leaf path with a sum equal to k, return [[-1]].
The order of the paths (order of the lists in the outer list) does not matter.
Constraints:
"""
class BinaryTreeNode:
self.value = value
self.left = None
self.right = None
"""
"""
Args:
root(BinaryTreeNode_int32)
k(int32)
Returns:
list_list_int32
"""
return []
Problem
Output:
Example Two
Output:
Notes
Diameter of a binary tree is the length of the longest path between any two nodes of the
tree.
Length between any two nodes is equal to the number of edges traversed to reach one
node from the other.
Constraints:
"""
class BinaryTreeNode:
self.value = value
self.left = None
self.right = None
"""
def binary_tree_diameter(root):
"""
Args:
root(BinaryTreeNode_int32)
Returns:
int32
"""
return 0
Problem
Example One
Output:
Example Two
Output:
Notes
A binary tree is called height-balanced if for any node, the difference in the heights of its
left and right subtree does not exceed one.
Input list does not contain duplicates.
Return the root node of the created hight-balanced BST.
Constraints:
1 <= length of the linked list <= 20000
-109 <= node value <= 109
"""
class LinkedListNode:
self.value = value
self.next = None
class BinaryTreeNode:
self.value = value
self.left = None
self.right = None
"""
def sorted_list_to_bst(head):
"""
Args:
head(LinkedListNode_int32)
Returns:
BinaryTreeNode_int32
"""
return None
Problem
Construct A Binary Search Tree From Its
Preorder Traversal
Construct a Binary Search Tree whose preorder traversal matches the given list.
Example One
{
"preorder": [1, 0, 2]
}
Output:
Example Two
{
"preorder": [2, 0, 1, 3, 5, 4]
}
Output:
Notes
Constraints:
"""
class BinaryTreeNode:
self.value = value
self.left = None
self.right = None
"""
def build_binary_search_tree(preorder):
"""
Args:
preorder(list_int32)
Returns:
BinaryTreeNode_int32
"""
return None