For which test case is my code failing

Unable to find out then failing scenario

hello @himesh.sharma25

u are never reading the given sum , u are always passing 5.

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.

Didnt get what you are saying

import java.util.*; public class Main { static class INT{ int v; INT(int v){ this.v = v; } } private static int subTreeWithGivenSum(BinaryTreeNode root, int x, INT count){ if(root == null){ return 0; } int sumLSubTree = subTreeWithGivenSum(root.leftChild, x , count); int sumRSubTree = subTreeWithGivenSum(root.rightChild, x, count); int sum = root.data + sumLSubTree + sumRSubTree; if(sum == x){ count.v++; } return sum; } private static int countTreeWithGivenSum(BinaryTreeNode root, int x){ if(root == null){ return 0; } INT count = new INT(0); int countLSubTree = subTreeWithGivenSum(root.leftChild, x, count); int countRSubTree = subTreeWithGivenSum(root.rightChild, x, count); if((root.data + countLSubTree + countRSubTree) == x){ count.v++; } return count.v; } public static void main (String args[]) { Scanner s = new Scanner(System.in); int t = s.nextInt(); for (int k = 0; k< t ;k ++){ String tree = s.next(); int x = s.nextInt(); BinaryTreeNode root = stringToTree(tree); int result = countTreeWithGivenSum(root, x); System.out.println(result); } } private static BinaryTreeNode stringToTree(String s){ int count = 0; String[] arr = s.split(" "); BinaryTreeNode head = new BinaryTreeNode<>(Integer.parseInt(arr[count])); Queue<BinaryTreeNode> queue = new LinkedList<>(); queue.add(head); while(!queue.isEmpty()){ BinaryTreeNode front = queue.poll(); if(count < arr.length - 1){ count++; front.leftChild = new BinaryTreeNode<>(Integer.parseInt(arr[count])); queue.add(front.leftChild); } count++; if(count < arr.length){ front.rightChild = new BinaryTreeNode<>(Integer.parseInt(arr[count])); queue.add(front.rightChild); } } return head; } } class BinaryTreeNode { T data; BinaryTreeNode leftChild; BinaryTreeNode rightChild; public BinaryTreeNode(T data) { this.data = data; leftChild = null; rightChild = null; } }

pls save ur code to cb ide , and then share its link.

check now->

the issue was with reading input.

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.