Introduction
Data structures are one of the most fundamental concepts in object-oriented programming. To explain it simply, a data structure is a particular way of organizing data in a computer so that it can be effectively processed. There are several data structures like stacks, queues, and trees that have their own unique properties.
Trees allow us to organize data in a hierarchical fashion. Such a data structure is very different from linear data structures like linked lists or arrays. A tree consists of nodes that carry information.
A binary tree is a special type of tree that can only have up to two children. This means that a particular node in a binary tree can have no child, one child, or two children but not more. A binary tree is an important data structure that can enable us to solve difficult problems and build complex codes.
If you are applying for a job as a Java Developer or a software engineer, your interview may contain several questions revolving around this concept. Often, candidates find it hard to answer questions based on binary trees, binary search trees, and related programs. In this article, we will explore some of the most frequently asked interview questions related to binary trees. This article will help you better understand the concept and prepare you so that you can land your dream job!
Top Binary Tree Interview Questions & Answers
The following section contains a catalog of questions and their expected answers based on the binary tree concept.
1) What is a leaf node?
Any node in a binary tree or a tree that does not have any children is called a leaf node.
2) What is a root node?
The first node or the top node in a tree is called the root node.
3) How do you find the lowest common ancestor (LCA) of a binary tree in Java?
Let us consider two nodes n1 and n2 that are part of a binary tree.
The lowest common ancestor (LCA) of n1 and n2 is the shared ancestor of n1 and n2 that is located farthest from the root.
You can follow the following method to find the LCA.
- a) Find a path from the root node to n1 and store it in an array.
- b) Find a path from the root node to n2 and store it in an array.
- c) Traverse both paths until the value is the same in both the arrays.
4) How do you check if a given binary tree is a subtree of another binary tree?
Consider we have a binary tree T. We now want to check if a binary tree S is a subtree of T.
To do this, first, try to check if you find a node in T that is also in S.
Once you find this common node, check if the following nodes are also a part of S.
If yes, we can safely say that S is a subtree of T.
5) How do you find the distance between two nodes in a binary tree?
Consider two nodes n1 and n2 that are part of a binary tree.
The distance between n1 and n2 is equal to the minimum number of edges that need to be traversed to reach from one node to the other.
It is important to note that you traverse the shortest distance between the nodes.
6) What is a binary search tree?
A binary search tree (BST) is a special type of binary tree in which each internal node contains a key. For a binary search tree, the rule is:
- a) A node can have a key that is greater than all the keys in the node’s left subtree.
- b) A node can have a key that is smaller than all the keys in the node’s right subtree.
Thus, if n1 is a node that has a key 8, then every node in the left subtree of n1 will contain keys lesser than 8, and every node in the right subtree of n1 will contain keys greater than 8.
7) What is a self-balanced tree?
Self-balanced binary search trees automatically keep their height as small as possible when operations like insertion and deletion take place.
For a BST to be self-balanced, it is important that it consistently follows the rules of BST so that the left subtree has lower-valued keys while the right subtree has high valued keys.
This is done using two operations:
– Left rotation
– Right rotation
8) What is an AVL tree?
The AVL tree is named after its inventors: Adelson, Velski, and Landis. An AVL tree is a self-balancing binary tree that checks the height of its left subtree and right subtree and assures that the difference is not more than 1. This difference is called the balance factor
Thus, BalanceFactor = height (Left subtree) – height (Right subtree)
If the balance factor is more than 1, the tree is balanced using some of the following techniques:
– Left rotation
– Right rotation
– Left-Right rotation
– Right-Right rotation
9) How do you convert a binary tree into a binary search tree in Java?
The main difference between a binary tree and a binary search tree is that the BST follows the left subtree should have lower key values and the right subtree should have higher key values rule. This can be done using a series of traversal techniques as follows:
- Create a temporary array that stores the inorder traversal of the tree
- Sort the temporary array. You can use any sorting algorithm here.
- Again perform an inorder traversal on the tree.
- Copy the array elements one by one to each tree node.
10) How do you delete a node from a binary search tree in Java?
The deletion operation for a BST can be tricky since its properties need to be preserved post the operation. Here’s a look at all three possible cases:
- Node to be deleted is a leaf node.
Simply remove the node. - Node to be removed has one child.
In this case, copy the child to the node and delete the child.
- Node to be removed has two children.
In this case, find the inorder successor of the node. You can then copy its content to the node and delete the inorder successor.
11) What is the Red-Black tree data structure?
The Red-Black tree is a special type of self-balancing tree that has the following properties:
- Each node has a colour either red or black.
- The root is always black.
- A red node cannot have a red parent or red child.
- Every path from the root node to a NULL node has the same number of black nodes.
12) How do you find if two trees are identical?
Two binary trees are identical if they have the same data and arrangement. This can be done by traversing both trees and comparing their data and arrangements.
Here’s the algorithm that can enable us to do this:
- Check data of root node ( tree1 data ==tree2 data)
- Check left subtree recursively. call sameTree( tree1-> left subtree, tree2-> left subtree)
- Similarly, check right subtree
- if a,b,c are true, return1
Final Thoughts
In this article, we explored some of the most commonly asked binary search tree interview questions. Exploring more about data structures can help you get a better grasp of logic and programming. You can try looking at examples mentioned in this article and practice by changing values to build your fundamentals. With some practice, you will be in a great position to crack your interview.
If you are curious to learn about data science, check out IIIT-B & upGrad’s PG Diploma in Data Science which is created for working professionals and offers 10+ case studies & projects, practical hands-on workshops, mentorship with industry experts, 1-on-1 with industry mentors, 400+ hours of learning and job assistance with top firms.
This article was originally published on upGrad.