Top Binary tree interview problems
Here is a curated list of top interview problems on binary trees. These are mostly asked in face to face technical interview rounds. The list contains a wide variety of problems collected from various sources including leetcode, codeforces, codechef, hackerrank and other programming platforms. Practicing these questions would be sufficient enough to brush up your
Check if a binary tree is a subtree of another binary tree
You are given the root nodes of two trees T and S, you need to check whether S is a subtree of T or not. A tree S is a subtree of a tree T if S consists of a node in T and all of its descendants. Consider the example below. Tree T 26
Construct a BST from preorder traversal
You are given a preorder traversal, you need to construct a binary search tree. Input: {10, 5, 1, 7, 40, 50} is the preorder traversal. Output: 10 / \ 5 40 / \ \ 1 7 50 Algorithm We know that the first element of a preorder traversal if the root of the tree. Leveraging
Left view of a binary tree
You are given a binary tree, you need to print the left view of it. Consider the tree in the below figure. The left view of the tree would be {8, 3, 1, 4}. Algorithm Create a queue which would store the nodes of the tree. Insert the root node in the tree. While the
Construct a binary tree from preorder and postorder traversals
You are given preorder and postorder traversals, you need to construct the binary tree. Input: Preorder = {1, 2, 4, 8, 9, 5, 3, 6, 7} Postorder = {8, 9, 4, 5, 2, 6, 7, 3, 1} Output: 1 / \ 2 3 / \ / \ 4 5 6 7 / \ 8 9
Construct a binary tree from inorder and preorder traversals.
You are given inorder and preorder traversals, you need to construct the binary tree. Input: Inorder = {4, 2, 5, 1, 3, 6} Preorder = {1, 2, 4, 5, 3, 6} Output: 1 / \ 2 3 / \ \ 4 5 6 Algorithm: We know that, the first index of a preorder traversal contains
Construct a binary tree from inorder and postorder traversals
You are given inorder and postorder traversals, you need to construct the binary tree. Input : Inorder = {4, 8, 2, 5, 1, 6, 3, 7} Postorder = {8, 4, 5, 2, 6, 7, 3, 1} Output : 1 / \ 2 3 / \ / \ 4 5 6 7 \ 8 Algorithm We
Path sum equal to k in trees
You are given a binary tree which can have negative elements also and an integer k, you need to print all the paths in the tree which has sum equal to k. Note that the paths need not necessarily be from root to leaf nodes. It can start from any node and can end to
Number of Islands
You are given a 2D grid of 1’s and 0’s where 1’s denotes the land and 0’s denote the water. You need to return the number of islands. An island is surrounded by water and is formed by connecting adjacent lands either vertically or horizontally. You can assume that all the four edges of the
Product of Array except self
You are given an array containing n integers (n>1), you need to return an output array such that output [i] is equal to the product of all the elements of the array except the i’th element. Input: [1, 2, 3, 4] Output: [24, 12, 8, 6] Constraint: You need to solve the problem without using
Loading…
Something went wrong. Please refresh the page and/or try again.
Follow My Blog
Get new content delivered directly to your inbox.