Server Side Programming Articles - Page 2184 of 2650

Print the nodes of binary tree as they become the leaf node in C++ Programming.

Sunidhi Bansal
Updated on 04-Sep-2019 07:29:57

167 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

Print matrix in zag-zag fashion in C Programming.

Sunidhi Bansal
Updated on 04-Sep-2019 07:22:21

1K+ Views

Given a matrix mat[row][col] we have to print the given matrix in zig-zag fashion like in the given image below −So the output should be like −Output: 10 20 40 70 50 30 60 80 90For the above problem, we have followed a simple approach where we have to iterate the matrix diagonally and change the value of iteration to change the direction after every previous match.AlgorithmSTART STEP 1-> DECALRE AND SET k = 3, l = 3 STEP 2-> DECLARE A MATRIX mat[][3] STEP 3-> DECLARE AND SET row = 0, col = 0, flag = false; STEP 4-> ... Read More

Print path between any two nodes in a Binary Tree in C++ Programming.

Sunidhi Bansal
Updated on 04-Sep-2019 07:05:59

486 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

Print the first shortest root to leaf path in a Binary Tree in C++ Programming.

Sunidhi Bansal
Updated on 04-Sep-2019 06:57:54

248 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

Print root to leaf paths without using recursion in C++ Programming.

Sunidhi Bansal
Updated on 04-Sep-2019 06:45:51

261 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

Print the nodes at odd levels of a tree in C++ Programming.

Sunidhi Bansal
Updated on 04-Sep-2019 06:38:59

165 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

Print Levels of all nodes in a Binary Tree in C++ Programming.

Sunidhi Bansal
Updated on 04-Sep-2019 06:34:51

292 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

Print the number of set bits in each node of a Binary Tree in C++ Programming.

Sunidhi Bansal
Updated on 04-Sep-2019 06:29:29

185 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

Print level order traversal line by line in C++ Programming.

Aman Kumar
Updated on 30-Jul-2025 13:09:16

413 Views

Level Order Traversal, also known as Breadth-First Search (BFS), is a method of traversing a tree where nodes are visited level by level, starting from the root node and moving left to right within each level. In this article, our task is to print the nodes of a binary tree in level order, with each level displayed on a separate line. For example, if the binary tree (consider the below image) is traversed in level order. The output will look like this: 1 2 3 4 5 Printing Level Order Traversal Line by Line The following are the ... Read More

Print matrix in snake pattern from the last column in C Programming.

Sunidhi Bansal
Updated on 04-Sep-2019 06:13:45

374 Views

Given an array of nxn size, the program must print the elements of an array in a snake pattern starting from the last column that means from arr[0][n]th element without doing any changes to their original locations.ExampleInput: arr[]= 100 99 98 97    93 94 95 96    92 91 90 89    85 86 87 88 Output: 97 98 99 100 96 95 94 93 92 91 90 89 88 87 86 85AlgorithmSTART Step 1 -> declare initial variable as int n to 5, i and j Step 2 -> declare array of 2-D matrix with elements Step 3 -> Loop For i=0 and i= 0; j--)             printf("%d ", arr[i][j]);       }    return 0; }Outputif we run the above program then it will generate the following output50 40 30 20 10 60 70 80 90 100 150 140 130 120 110 160 170 180 190 200 250 240 230 220 210

Advertisements