Found 7347 Articles for C++

Delete middle of linked list in C++?

AmitDiwan
Updated on 16-Jan-2021 07:19:23

333 Views

Let us first define our linked list that contains data and the pointer to the next node.struct Node {    int data;    struct Node* next; };Next we create our createNode(int data) function that takes int data as parameter and returns the newly created node after assigning the parameter value. The next pointer to the node will be null.Node* createNode(int data){    struct Node* newNode = new Node;    newNode->data = data;    newNode->next = NULL;    return newNode; }Now we have our deleteMiddle(struct Node* head) function which takes the root node. If the root node isn’t null then it ... Read More

Delete leaf nodes with value k in C++?

AmitDiwan
Updated on 16-Jan-2021 07:13:20

81 Views

Let us first define the struct that would represent a tree node that contains the data and its left and right node child. If this is the first node to be created then it’s a root node otherwise a child node.struct Node {    int data;    struct Node *leftChild, *rightChild; };Next we create our newNode(int data) function that takes an int value and assign it to the data member of the node. The function returns the pointer to the created struct Node. Also, the left and right child of the newly created node are set to null.struct Node* newNode(int ... Read More

Delete leaf nodes with value as x in C++?

AmitDiwan
Updated on 16-Jan-2021 07:05:24

127 Views

Let us first define the struct that would represent a tree node that contains the data and its left and right node child. If this is the first node to be created then it’s a root node otherwise a child node.struct Node {    int data;    struct Node *leftChild, *rightChild; };Next we create our newNode(int data) function that takes an int value and assign it to the data member of the node. The function returns the pointer to the created struct Node. Also the left and right child of the newly created node are set to null.struct Node* newNode(int ... Read More

Delete array element in given index range [L – R] in C++?

AmitDiwan
Updated on 16-Jan-2021 06:58:22

489 Views

Let us first define the original array and the exclusive range for deletion of the array elements and also find the original array length −int arr[] = { 2, 4, 6, 8, 10, 12, 14, 16, 18, 20}; int L = 2, R = 6; int length = sizeof(arr) / sizeof(arr[0]);Now we loop in the array and if index position (i) is greater than L or R we increment the variable k which will be used to shift positions(delete) of array element once the index value (i) lies between the range L and R. Also, the new length of the ... Read More

Delete an element from array using two traversals and one traversal in C++?

AmitDiwan
Updated on 16-Jan-2021 06:48:41

75 Views

Two Traversals Let us first define the original array and the element to be searched and deleted from the array −int ele = 5; int arr = [1,2,3,4];Now we loop in the array to find the given element −for (i=0; i

Count the Number of Binary Search Trees present in a Binary Tree in C++

Sunidhi Bansal
Updated on 07-Jan-2021 07:03:44

1K+ Views

We are given a binary tree as input. The goal is to find the number of binary search trees (BSTs) present as subtrees inside it. A BST is a binary tree with left child less than root and right child more than the root.For ExampleInputThe tree which will be created after inputting the values is given below −OutputCount the Number of Binary Search Trees present in a Binary Tree are: 2Explanationwe are given with an array of integer values that is used to form a binary tree and we will check whether there is a binary search tree present in ... Read More

Construct Special Binary Tree from given Inorder traversal in C++

Sunidhi Bansal
Updated on 07-Jan-2021 06:59:43

345 Views

We are given an array arr[] containing the inorder traversal of a binary tree. The goal is to construct a special binary tree from that array. A special binary tree is the one which has root node’s weight greater than the weights of both its left and right children.For ExampleInputint arr[] = {10, 20, 28, 40, 32, 31, 30}OutputThe special binary tree which will be constructed with the given inorder traversal is given below −Explanationwe are given with an array of integer values or the inorder traversal of a tree. So, the special tree formed is 10, 20, 28, 40, ... Read More

Count pairs in a binary tree whose sum is equal to a given value x in C++

Sunidhi Bansal
Updated on 07-Jan-2021 06:55:14

158 Views

We are given an integer value and a variable x and the task is to construct the binary tree and find the pairs having sum equals to the given value x.For ExampleInputint x = 5, The tree which will be created after inputting the values is given below −OutputCount of pairs in a binary tree whose sum is equal to a given value x are: 2Explanationwe are given with an array of integer values that is used to form a binary tree and we will check whether there is a pair present in a binary tree whose sum equals to ... Read More

Construct the full k-ary tree from its preorder traversal in C++

Sunidhi Bansal
Updated on 07-Jan-2021 06:44:20

317 Views

We are given an array arr[] containing the preorder traversal of the k-ary tree in sequence. The goal is to construct the same k-ary tree from it and print its postorder traversal. A full k−ary tree is the one in which the root node has 0 or k children i.e. at most k child.For ExampleInputint arr[] = {2, 5, 1, 3, 6, 7, 2, 1 }, int size = 8, int children = 2OutputThe full k−ary tree which will be constructed with the two children from preorder traversal is given below −Explanationwe are given with an array of integer values ... Read More

Count the nodes whose sum with X is a Fibonacci number in C++

Sunidhi Bansal
Updated on 07-Jan-2021 06:37:55

93 Views

Given a binary tree with weights of its nodes as numbers. The goal is to find the number of nodes that have weights such that the number is a Fibonacci number. Numbers in Fibonacci series are: 0, 1, 1, 2, 3, 5, 8, 13….nth number is the sum of (n−1)th and (n−2)th. If weight is 13 then it is a Fibonacci number so the node will be counted.For ExampleInputtemp =1. The tree which will be created after inputting the values is given below −OutputCount the nodes whose sum with X is a Fibonacci number are: 3Explanationwe are given with the ... Read More

Advertisements