Dont know where to start?

Can you please share the code regarding how to start about how to take inputs or do we have to make our own tree class??

hey @bhavik911
I think the generic tree will be added to your course

it is added.
But I don’t how to get input like this

I mean using the integers given

exactlly generic tree , no of children will not exceed 2.

No i mean I dont understand how to take input in that we used true and false , should i use loop here?

@bhavik911
use loop here

package Trees;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.Scanner;

public class SumAtLevelK {
    static Scanner scanner = new Scanner(System.in);

    public static void main(String[] args) {

        BT tree = new BT();
        int k = scanner.nextInt();
        System.out.println(tree.sumK(k));


    }

    static class BT {
        private Node root;

        private class Node {
            int data;
            ArrayList<Node> children;

            public Node() {
                children = new ArrayList<>();
            }

            public Node(int data) {
                this.data = data;
                children = new ArrayList<>();
            }
        }

        public BT() {

            root = construct(null, -1);
        }


        private Node construct(Node parent, int ith) {
            //root

            //input
            int item = scanner.nextInt();
            //New node
            Node nn = new Node(item);
            //Enter no of child
            int no_of_child = scanner.nextInt();
            for (int i = 0; i < no_of_child; i++) {
                Node child = construct(nn, i);
                nn.children.add(child);
            }

            return nn;

        }

        public void display() {
            System.out.println("-------------");
            display(root);
            System.out.println("-------------");
        }

        private void display(Node node) {
            String str = node.data + "-> ";
            for (Node child : node.children) {
                str += child.data + " ";
            }
            str += ".";
            System.out.println(str);

            for (Node child : node.children) {
                display(child);
            }
        }

        public int sumK(int k) {
            return sumK(root, k);
        }

        private int sumK(Node root, int k) {
            LinkedList<Node> ll = new LinkedList<>();
            ll.add(root);
            int count = 0;
            int sum = 0;
            while (count <= k && !ll.isEmpty()) {
                Node rv = ll.removeFirst();
                if (count == k) {
                    sum += rv.data;
                    while (!ll.isEmpty()) {
                        Node sv = ll.removeFirst();
                        sum += sv.data;
                    }
                    return sum;
                }
                if (rv.children.size() != 0) {
                    count+=1;
                    for (int i = 0; i < rv.children.size(); i++)
                        ll.addLast(rv.children.get(i));
                }
            }
            return sum;
        }

    }
}

Can you tell tell whats the error here , the answer is not right

you are doing wrong
debug your code for custom input :
sum at level 2
Here the tree looks like

                     1                                 Level 0
                /          \
              2              5                         Level 1
           /      \       /     \
          3       4      6        7                    Level 2 

You are adding 5 3 and 4
it is simple recursion based Question

What should be the change in my code , i dont get it?

please reply what should i change?

Logic is not correct .
it is simple to try recursive solution

Hey , What should be the right logic for it , I am usimg queues but not able to get the right logic I think

static int sum = 0;

private void SumAtLevel(Node node, int level, int cl) { // level is k and cl is current level
if (cl == level) {
sum += node.data;
}
for (Node Child : node.children) {
SumAtLevel(Child, level, cl + 1);
}

}

Hey but taking a static variable isnt always a preferred option , as it was mentioned in the video, How should I do it without this

public int SumAtLevel(int level) {
return	SumAtLevel(this.root, level, 0);

}

private int SumAtLevel(Node node, int level, int cl) {
int sum=0;
if (cl == level) {
sum += node.data;
}

	for (Node Child : node.children) {
        sum +=SumAtLevel(Child, level, cl + 1);
	}
    return sum;

}