Tag Archives: Tree

Find a Pair with the given sum in a BST

Problem Given a binary tree, the task is to find a pair with the given sum. The elements of the tree are pairwise distinct. Sample Input Sum = 11 Sample Output Pair is (4, 7) Approach We will use an unordered set to solve this problem. This is because an unordered set gives the time complexity of O(1)… Read More »

Print a two-dimensional view of a Binary Tree

Problem Given a binary tree, the task is to print the two-dimensional view of it. Sample Input             1            /  \           9    3           / \   / \          2   5  6  10  Sample Output:                    10          3                    6 1                  5          9                    2 Approach This problem is based on the observations that we can make from the examples. Below… Read More »

Create a mirror of an m–ary Tree

Problem Given an m-ary tree that can have a count of children greater than 2. Convert it into its mirror tree. Sample Input Sample Output Approach We can use “Depth First Search” to solve this problem. We can first store all the children of each node in a list. We traverse the given tree in any order and… Read More »

Construction of an Expression Tree

Problem Given a postfix expression, the task is to generate an expression tree from it and return the infix notation of the expression tree. Example: Below is the expression tree for the given expression: 2 + 3 Sample Input 2 3 + Sample Output The infix expression is 2 + 3 Explanation Below is the expression tree for… Read More »

Tree vs Graph: Notable Differences You need to Know

Programmers are often interested in the Tree vs Graph comparison. To begin with, know that trees and graphs both are non-linear, non-primitive types of data structure. Both use nodes to represent many structures that are used to solve real-world problems albeit differently. The tree uses the hierarchical model to represent its structure, whereas a graph uses the network… Read More »

Types of Data Structure

In programming, we use so many types of data structures to provide a proper structure to our data. A data structure is basically used to collect and organize the data on which we are going to perform the operations. For example, we all know what is an array, it is one of the most-used data structures. We use… Read More »