- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Found 7347 Articles for C++
![Sunidhi Bansal](https://www.tutorialspoint.com/assets/profiles/124656/profile/60_131452-1581053210.jpg)
84 Views
Given a binary tree, we have to print its leaf nodes and then we have to remove those leaf nodes and then repeat till there are no nodes left in the tree.ExampleSo the output of the problem should be −6 7 9 13 14 3 4 2 1ApproachWe have adopted an approach where we are applying DFS.For applying a temporary value zero is assigned to every value and then assign all the nodes with the value maximum(value of both child)+1.AlgorithmSTART STEP 1-> DEFINE A struct Node WITH DATA MEMBERS data, order, *left, *right STEP 2-> DEFINE A struct Node* ... Read More
![Sunidhi Bansal](https://www.tutorialspoint.com/assets/profiles/124656/profile/60_131452-1581053210.jpg)
369 Views
We are given with a binary tree of distinct nodes and two nodes of the binary tree whose path in the binary tree we want to print.For Example − We want to print the path between node 140 to 211 so its output should be like −Output: 140->3->10->211The idea is to find paths form root nodes to the two nodes and store them in two separate vectors or arrays say path1 and path2.Now, there arise two different cases −If the two nodes are in different subtrees of root nodes − When the two nodes are in different subtrees like one ... Read More
![Sunidhi Bansal](https://www.tutorialspoint.com/assets/profiles/124656/profile/60_131452-1581053210.jpg)
96 Views
Given the binary tree the program must find out the shortest path from the root to leaf amongst many given paths.Since we traverse the tree from left to right, so if there are multiple shortest paths from the root to leaf than the program will print the first traversed the shortest path on the left side of a tree.We can use a queue that will traverse each level using Level order traversal and the path with the least number of levels will be printed as it will be the shortest path from the root to leafIn the above tree, multiple ... Read More
![Sunidhi Bansal](https://www.tutorialspoint.com/assets/profiles/124656/profile/60_131452-1581053210.jpg)
142 Views
Given the binary tree the program must find out the multiple paths from the root to a leaf which means all the paths should be printed but the challenge is to without using recursion.We will traverse the tree iteratively as the constraint is to do it without recursion. So to achieve this we can use an STL map that will store the root element and whenever the leaf node is identified through level order traversal it will print the path from root to leaf as there is a map pointer which is pointing to a root node.In the above tree, ... Read More
![Sunidhi Bansal](https://www.tutorialspoint.com/assets/profiles/124656/profile/60_131452-1581053210.jpg)
90 Views
Given the binary tree, the program must print the nodes at odd levels of a tree and the levels of a binary tree start from 1 to n.As nothing is mentioned one of the two approaches can be implemented i.e. recursion or iteration.Since we are using a recursive approach, the program will make a recursive call to a function that will be fetching the nodes at odd levels and returning them.In the above binary tree −Nodes at level 1: 10 Nodes at level 2: 3 and 211 Nodes at level 3: 140, 162, 100 and 146So, the nodes at level ... Read More
![Sunidhi Bansal](https://www.tutorialspoint.com/assets/profiles/124656/profile/60_131452-1581053210.jpg)
183 Views
Given the binary tree, the task is to print the level associated with every key stored in a node starting from 1 to nIn the above tree, nodes are −10 at level 1 3 and 211 at level 2 140, 162, 100 and 146 at level 3Given the key the program must print the level of that particular key.ExampleInput: 10 3 211 140 162 100 146 Output: level of 10 is 1 Level of 3 is 2 Level of 211 is 2 Level of 140 is 3 Level of 162 is 3 Level of ... Read More
![Sunidhi Bansal](https://www.tutorialspoint.com/assets/profiles/124656/profile/60_131452-1581053210.jpg)
86 Views
Given the binary tree, the function will generate the binary values of the keys stored in the nodes and then return the number of set bits(1) in that binary equivalent.ExampleBinary tree having keys as: 10 3 211 140 162 100 and 146KeyBinary equivalentSet bits(output)101010230011221111010011514010001100316210100010310011001003146100100103Here we are using the function __builtin_popcountThe function prototype is as follows −int __builtin_popcount(unsigned int)It returns the numbers of set bits in an integer i.e. the number of ones in the binary representation of the integer.AlgorithmSTART Step 1 -> create a structure of a node as struct Node struct node *left, *right ... Read More
![Sunidhi Bansal](https://www.tutorialspoint.com/assets/profiles/124656/profile/60_131452-1581053210.jpg)
293 Views
Given the binary tree, the function must find out the level order traversal of a tree line by line.Level order traversal: Left Root Right, which means firstly print left child of a node than the value of a root and then go to the right child but here we have to do line by line which will start from left and will end at the right node of a given binary tree.The binary tree given above will generate the following output −Level 0: 3 Level 1: 2 1 Level 2: 10 20 30AlgorithmSTART Step 1 -> create a structure of ... Read More
![Sunidhi Bansal](https://www.tutorialspoint.com/assets/profiles/124656/profile/60_131452-1581053210.jpg)
149 Views
Program should print the leaf nodes of a binary tree from left to right but the challenge involves is using of only one stackThrough push() function insert nodes of a binary tree and through pop() operation display the leaf nodes.Leaf nodes are the end nodes whose left and right pointer is NULL which means that particular node is not a parent node.ExampleInput : 12 21 32 41 59 33 70 Output : 41 59 33 70Stack is a data structure which is a LIFO structure in which top pointer will point to the last elements inserted so the leaf nodes ... Read More
![Arnab Chakraborty](https://www.tutorialspoint.com/assets/profiles/123881/profile/60_2940367-1661162113.png)
100 Views
Here we will see one interesting problem. Suppose one value of n is given. We have to find all strings of length n, such that there are no consecutive 1s. If n = 2, then the numbers are {00, 01, 10}, So output is 3.We can solve it using dynamic programming. Suppose we have a tables ‘a’ and ‘b’. Where arr[i] is storing the number of binary strings of length i, where no consecutive 1s are present, and ending with 0. Similarly, b is holding the same but numbers ending with 1. We can append 0 or 1 where last ... Read More