java - Use recursion to count the number of nodes whose value field is between min and max, inclusive, from the "cur" node to the end of the list -
my attempt @ solution, know not right because output program incorrect. doing wrong?
i have inner node class, each value fields.this method should return number of nodes have value fields between ints min , max.
//---------------- countinrange( node, int, int ) ------------------ private int countinrange( node cur, int min, int max ) { if(cur == null) return 0; else { if(cur.value >= min && cur.value <= max) return (1+ countinrange(cur.next, min, max)); } return 1; }
the problem recursive call if value in range, otherwise pretend remainder of list has 1 element in range.
you need recursive call whether value in range or not. difference whether add 1 result or not before returning it.
Comments
Post a Comment