Skip to main content

Posts

Construct Binary Tree from preorder and inorder | Data Structure

Introduction: In this tutorial we are going to see how we can construct the binary tree from given preorder and inorder. Prerequisites: you should know about binary tree traversal and on paper you can draw binary tree from given preorder and inorder traversal. Inorder:left->root->right; Preorder:root->left->right; Problem Statement: we have given two arrays. one for preorder and another for inorder. by using these two array we have to built a binary tree. eg: preorder = [3,9,20,15,7] inorder = [9,3,15,20,7] solution: Solution: We will follow recursive approach to solve this question.let's discuss how we can solve it. Trick: In the given preorder the very first element will be the root of the tree. then we will find root element in inorder also. and we know in inorder traversal we have left part then root and then right part of the tree. by using preorder we can get the root of the main tree and by using inorder and root we can get the left part and right part of the...

Intersection of Two Linked List | Linked List Data Structure

Problem Statement: Write a program to find the node at which the intersection of two singly linked lists begins. Input: intersectVal = 8, listA = [4,1,8,4,5], listB = [5,6,1,8,4,5], skipA = 2, skipB = 3 Output: Reference of the node with value = 8 Input Explanation: The intersected node's value is 8 (note that this must not be 0 if the two lists intersect). From the head of A, it reads as [4,1,8,4,5]. From the head of B, it reads as [5,6,1,8,4,5]. There are 2 nodes before the intersected node in A; There are 3 nodes before the intersected node in B. Solution: Use Hashmap. first of all create a hashmap, in which we store the nodes of first list. then iterate over second linked list and search each node of the second linked list into the hashmap, if we will find the node into hashmap, we will stop here. and store that node into the solution variable. Full Code: ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) { map<ListNode*,int>m; ListNode* s...

Merge Sort On Linked List | Data Structure in C++

Introduction: Merge Sort: Merge Sort is basically a sorting technique to sort the data. so basically we use it with array.but today we will apply it in linked list. Problem Statement: We have given a linked list, and we have to sort it using any sorting technique. eg: Input: 1->4->5->2->NULL       Output: 1->2->4->5->NULL Solution: To apply merge sort on linked list, we will maintain three function. 1.we should have a function to find the middle of the linked list.(findMid function) 2.we should have a function which will break the linked list into two parts.(mergeSort function) 3.we should have a function which will merge two list in sorted order.(merge function) Important Point: first of all we have to find the tail of the list. and then we will pass head as well as tail to our merge sort function. kindly go through the code. you will understand each function easily. Full Code: Merge Sort In C++ On Linked List /**  * Definition for ...

C++ Basic Programs | Check Palindrome

Introduction: In this tutorial, we are going to solve a basic problem on string. and this is really an important problem to understand few concepts related to problem solving.basically today we are going to solve a problem in which we will try to find whether a given string is palindrome string or not? Problem Statement: We have given a string s. and then we have to check whether it is palindrome string or not? Input:string s = "sana" Output: "No" Input: string s = "abba" Output: "yes" Solution: let's first of all understand, what palindrome means? Anything is palindrome if we get the same result from left view and right view. eg: "abba" is palindrome, cause you see it from right or left, it will be same. eg: "Abba" is not a palindrome, cause from left it starts with "A", while from right, it starts with "a". we can solve this problem by two methods: 1.Using two pointer(easy one) 2.Using Stack(it will t...

Linked List Data Structure | Add Two Number Explanation

Introduction: In this tutorial, we are going to solve a very famous problem on Linked List. It is also an important question for coding interview. so let's see the problem and understand it in detail. Problem Statement: There are two numbers and each digit of number is represented by a node of linked list.and we have to add these two numbers, which is given in the form of linked list. Input: (2 -> 4 -> 3) + (5 -> 6 -> 4) Output: 7 -> 0 -> 8 eg: num1 = 2354;       num2 = 875; see the image below: Solution: Now let's solve it. In this problem there may be three possibilities. First: Number of Nodes in the first linked list is greater than second linked list. Second: Number of Nodes in first linked list is less than second list. Third: Number of Nodes in the both linked list is equal. Here number of nodes means number of digits in both number. Another thing we have to keep in mind is that if we add two digits then there may be possibility of gen...

Rotate Image Solution | Leetcode (In-place)

Introduction: In this tutorial we will try to solve Rotate Image problem from Leetcode. it is an important question from coding interview point of view. so let's try to solve it. Problem Statement: We have give a n*n 2D matrix. and we have to rotate it by 90 degree in Clockwise direction. Actually if you know little bit about Image Processing, then You already know that for manipulation or processing purpose we represent any image in the form of a 2D matrix. that's why the name of the problem is Rotate Image, which is quite relatable. Given input matrix = [ [ 5, 1, 9,11], [ 2, 4, 8,10], [13, 3, 6, 7], [15,14,12,16] ], Output: [ [15,13, 2, 5], [14, 3, 4, 1], [12, 6, 8, 9], [16, 7,10,11] ] Solution: whenever you find these type of problem related to 2D matrix. then think in this way. In any 2D matrix there will be four corner points, leftTop, rightTop, leftDown, rightDown. so we have to keep track of these four points. and we need an extra variable which will kee...

Validate Binary Search Tree | Leetcode(easy solution)

Introduction: In this tutorial we will try to solve this question from leetcode. and this is also an important question for coding interview. so let's see this problem and try to solve it. Problem Statement: We have given a Binary Tree. and then we have to find out whether it is also Binary Search Tree or Not? I hope you understand the difference between Binary Tree and Binary Search Tree. Solution: There are many ways to solve this problem, but we will see the most easy and efficient way. So if you know about different kind of traversal(Inorder, Preorder and Postorder) of Binary Tree, then you can easily solve this problem. To solve this problem, we will use Inorder traversal. In Inorder traversal we first traverse the left part then root and then right part of the tree. Trick: Inorder traversal of Binary Search Tree will be in sorted order. it means, if we find the Inorder traversal of any Binary Search Tree, then it will be in sorted order. so we will use this trick to solve th...