- Related Questions & Answers
- Count the nodes in the given tree whose sum of digits of weight is odd in C++
- Count the nodes whose weight is a perfect square in C++
- Count the nodes of the tree whose weighted string contains a vowel in C++
- Count the number of nodes at given level in a tree using BFS in C++
- Count the nodes whose sum with X is a Fibonacci number in C++
- XOR of all the nodes in the sub-tree of the given node in C++
- Count Complete Tree Nodes in C++
- Find the sum of left leaf nodes of a given Binary Tree in C++
- Count number of paths whose weight is exactly X and has at-least one edge of weight M in C++
- Count pairs in a binary tree whose sum is equal to a given value x in C++
- XOR of the path between any two nodes in a Binary Tree in C++
- Find sum of all nodes of the given perfect binary tree in C++
- Print all nodes between two given levels in Binary Tree in C++
- Python Program to Count Number of Non Leaf Nodes of a given Tree
- Count pairs from two linked lists whose sum is equal to a given value in C++

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

Given a binary tree with weights of its nodes. The goal is to find the number of nodes that have weights such that the number is power of two. If weight is 32 then it is 25 so this node will be counted.

**For Example**

The tree which will be created after inputting the values is given below −

Count the nodes in the given tree whose weight is a power of two are: 3

we are given with the tree node and the weights associated with each node. Now we calculate the power of each and every weight and check whether it can be expressed as power of 2 or not.

Node | Weight | Weight^2 | Power of 2 |
---|---|---|---|

2 | 8 | 2*2*2 | no |

1 | 100 | 10*2 | yes |

4 | 211 | Prime number | no |

3 | 16 | 4^2 | yes |

8 | 1717 | Not possible | no |

9 | 32 | 2*2*2*2*2 | yes |

The tree which will be created after inputting the values is given below −

Count the nodes in the given tree whose weight is a power of two are: 3

we are given with the tree node and the weights associated with each node. Now we calculate the digit sum of each and every weight and check whether it's odd or not.

Node | Weight | Weight ^ 2 | Power of 2 |
---|---|---|---|

2 | 16 | 4^2 | yes |

1 | 141 | Not possible | no |

4 | 41 | Prime number | no |

3 | 64 | 8^2 | yes |

8 | 81 | 9^2 | yes |

**Approach used in the below program is as follows** −

In this approach we will apply DFS on the graph of the tree to traverse it and check if the weight of nodes is power of 2. Take two vectors Node_Weight(100) and edge_graph[100] for this purpose.

Initialize Node_Weight[] with the weights of nodes.

Create a tree using vector edge_graph.

Take a global variable sum and initialize it with 0.

Function power_two(int node, int root) takes a node and root node of a tree and returns the count of nodes in the given tree whose weight is power of two.

Take set = Node_Weight[node];

If set && (!(set & (set − 1))) returns true then it is power of two (bitwise AND and then negation)

Increment powers as set has a value which is power of 2.

Traverse tree in vector edge_graph[node] using for loop.

Call power_two(it, node) for the next node in the vector.

At the end of all functions we will have power as the number of nodes with weights having value as power of two.

#include <bits/stdc++.h> using namespace std; vector<int> Node_Weight(100); vector<int> edge_graph[100]; int powers = 0; void power_two(int node, int root){ int set = Node_Weight[node]; if(set && (!(set & (set − 1)))){ powers++; } for(int it : edge_graph[node]){ if(it == root){ continue; } power_two(it, node); } } int main(){ //weight of the nodes Node_Weight[2] = 8; Node_Weight[1] = 100; Node_Weight[4] = 211; Node_Weight[3] = 16; Node_Weight[8] = 7171; Node_Weight[9] = 32; //create graph edge edge_graph[2].push_back(1); edge_graph[2].push_back(4); edge_graph[4].push_back(3); edge_graph[4].push_back(8); edge_graph[8].push_back(9); power_two(2, 2); cout<<"Count the nodes in the given tree whose weight is a power of two are: "<<powers; return 0; }

If we run the above code it will generate the following output −

Count the nodes in the given tree whose weight is a power of two are: 3

Advertisements