Skip to main content

Posts

Showing posts with the label tree

Tree Data Structure related must solve programming questions: Part - 2

Problem Statement: Given a tree return inorder traversal of a binary tree. Note: Solve it using both recursion and iterative approach. Approach - 1(Recursive approach): Recursive approach is very simple. Inorder traversal is traversing left sub tree first Then visiting root and finally visiting right sub tree. Break the recursion whenever there is no node available. Code in C++: class Solution {         void traverse(TreeNode* node, vector<int> &res){             if(!node)                 return;             traverse(node->left, res);                        res.push_back(node->val);           ...

LeetCode Problem: 1028. Recover a Tree From Preorder Traversal

Problem Statement: Recover binary tree from its given preorder traversal string. The string is given in the format: Dashes followed by value. The number of dashes convey its depth and value refers to the node value. Example 1: Input: string = "1-2--3--4-5--6--7"; Output: [1,2,5,3,4,6,7] Example 2: Input: string = "1-2--3---4-5--6---7"; Output: [1,2,5,3,null, 6, null, 4, null, 7] Example 3: Input: string = "1-401--349---90--88"; Output: [1,401,null,349,88,90]. Approach to the solution: Calculate value and its depth Check if right child is present. If yes then move to right child then go to step 2 again until depth of the node is reached. Else move to left and then go to step 2 again until depth of the node is reached. Once the calculated depth is reached. If left child of the node is null, create a new node and assign left child of the node. Else create a new node and assign it to right child. Continue from step 1 again until end of the string is reached. S...

Heap(priority queue) data structure

Heap is a data structure which is constructed based on the complete binary tree. A complete binary tree is a tree with all the nodes except leaf nodes are completely filled and all the nodes are left aligned. There are two types of heaps: 1)Max-Heap 2)Min-Heap 1)Max-Heap: Max heap is a data structure where the node has a value greater than all of its children. 2)Min-Heap Min heap is a data structure where the node has a minimum value compare to all of its children. In C++ STL there is a priority queue in the container which is the implementation of heap. It provides APIs for the heap data structure. Following are the APIs provided by C++ STL priority_queue : empty(): This function returns true if the priority queue is empty and false if the queue is not empty. Ex: priority_queue<int> pq;     pq.empty(); size(): This function returns the size of the priority queue. Here size means number of elements the priority queue is holding. If it is holding 10 elements it retur...

Leet Code: Problem #124 Binary Tree Maximum Path Sum

Given a non-empty binary tree, find the maximum path sum. For this problem, a path is defined as any sequence of nodes from some starting node to any node in the tree along the parent-child connections. The path must contain at least one node and does not need to go through the root. Example 1: Input: [1,2,3] 1 / \ 2 3 Output: 6 Example 2: Input: [-10,9,20,null,null,15,7]   -10    / \   9   20     /  \     15   7 Output: 42 Solution in C++: