Find sum at level k problem

public class Main {

public static void main(String[] args) {
    String tree = "3 + 4 + 6 + 7";
    int k = 0;
    System.out.println(sumATLevelK(tree, k) );
}



 static  int sumATLevelK(String tree,int k){
         if(tree==null){
              return 0;
         }
         if(k<=0){
             return root->data;

         }
           int leftAns =sumATLevelK(root->left,k-1);
         int rightAns =sumATLevelK(root->right,k-1);
         return leftAns+rightAns;
 }

}

check my code please help me?

First you need to check how you have stored your tree. Learn about different data structures that can be used to store trees. We generally use array or linked list to store trees NOT string.

Now you are using things like root->left Or root->right. Where have you defined the root.
You need to first define the root, Which is generally root node of tree in linked list representation.
You can’t use any variable name without declaring it.
If you dont get anything from above line, please read about linked list representation of trees and see few examples.

Thanks.

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.