input format is
1 2
2 2
3 0
4 0
5 2
6 0
7 0
2
how to build a tree please attach the code
How to take input of a generic tree
this is how to build a genric tree
@bishtmanish786
A generic tree is a tree where any node can have any number of children (unlike binary trees where a node can have atmost 2 children).
The first integer in each row indicates the data at the node whereas the second integer indicates the no of children that node has .
Refer to this code for the input function - https://ide.codingblocks.com/s/90995
As for the sample testcase given in the problem
1 2
2 2
3 0
4 0
5 2
6 0
7 0
2
Here the tree looks like
1 Level 0
/ \
2 5 Level 1
/ \ / \
3 4 6 7 Level 2
@tarunluthra aapke code k hisab se kisi bhi node k 2 se jyada child nhi ho skte to ye to ek binary tree hi ho gya na?
@bishtmanish786
In the Sum at Level K problem , Only the input function in this question is for generic tree. It is assured that you will get a binary tree as input only i.e. tree with nodes having atmost 2 children.
In a pure generic tree , the input function and node class are absolutely different. There the node class does not have left and right pointer for children , but rather we make an array or vector of pointers in which we store the locations of the children. The size of this array is equal to noOfChildren . Those trees are not covered in this course and to be honest , they are very rarely used in general. You will hardly find questions on them even on sites like HackerEarth and HackerRank so you can leave them and just focus on Binary Trees and Binary Search Trees.
As far as my code is concerned , yes you are right. It is just a code for Binary Tree which is being taken input as a Generic Tree.
I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.
On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.