- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- 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 a perfect square. If weight is 36 then it is 62 so this node will be counted.

**For Example**

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

Count the nodes whose weight is a perfect square are: 4

we are given with the tree nodes and the weights associated with each node. Now we check whether the digits of nodes are perfect squares or not.

Node | Weight | Perfect square | Yes/no |
---|---|---|---|

2 | 121 | 11*11 | yes |

1 | 81 | 9*9 | yes |

4 | 37 | Prime number | no |

3 | 25 | 5*5 | yes |

8 | 100 | 10*10 | yes |

9 | 701 | Not possible | no |

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

Count the nodes whose weight is a perfect square are: 2

we are given with the tree nodes and the weights associated with each node. Now we check whether the digits of nodes are perfect squares or not.

Node | Weight | Perfect square | Yes / No |
---|---|---|---|

2 | 11 | Not possible | no |

1 | 16 | 4*4 | yes |

4 | 4 | 2*2 | yes |

3 | 26 | Not possible | no |

8 | 1001 | Not possible | no |

**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 the node is a perfect square. 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 square and initialize it with 0.

Function check(int check_it) takes an integer and returns true if check_it is a perfect square.

Take total = sqrt(check_it)

Now if(floor(total) != ceil(total)) returns true then total is not a perfect square, return false.

Return true otherwise.

Function perfect_square(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 a perfect square.

If if(check(Node_Weight[node])) returns true, then increment square.

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

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

At the end of all functions we will have a square as the number of nodes with weights having value as a perfect square.

#include <bits/stdc++.h> using namespace std; vector<int> Node_Weight(100); vector<int> edge_graph[100]; int square = 0; bool check(int check_it){ double total = sqrt(check_it); if(floor(total) != ceil(total)){ return false; } return true; } void perfect_square(int node, int root){ if(check(Node_Weight[node])){ square++; } for (int it : edge_graph[node]){ if(it == root){ continue; } perfect_square(it, node); } } int main(){ //weight of the nodes Node_Weight[2] = 121; Node_Weight[1] = 81; Node_Weight[4] = 37; Node_Weight[3] = 25; Node_Weight[8] = 100; Node_Weight[9] = 701; //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); perfect_square(2, 2); cout<<"Count the nodes whose weight is a perfect square are: "<<square; return 0; }

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

Count the nodes whose weight is a perfect square are: 4

- Related Questions & Answers
- Count the nodes in the given tree whose weight is a power of two in C++
- Count of pairs in an array whose sum is a perfect square in C++
- Count the nodes in the given tree whose sum of digits of weight is odd in C++
- Count the nodes whose sum with X is a Fibonacci number in C++
- Count all triplets whose sum is equal to a perfect cube in C++
- Count numbers upto N which are both perfect square and perfect cube in C++
- Check if a number is perfect square without finding square root in C++
- Count the nodes of the tree whose weighted string contains a vowel in C++
- Count number of paths whose weight is exactly X and has at-least one edge of weight M in C++
- Count all pairs of adjacent nodes whose XOR is an odd number in C++
- Find the Next perfect square greater than a given number in C++
- Check Perfect Square or Not
- Check if given number is perfect square in Python
- 8086 program to find the square root of a perfect square root number
- Check for perfect square in JavaScript

Advertisements