Traversing un-ordered list and appending attibute values to the list item
Here are some tips:

1) Don't create variables if they're only used once or are a don't perform complex operations.

Old Code:

var num_sub_uls = $sub_uls.length;
var count_icount = 0;
if(num_sub_uls &gt; 0) {


New Code:

var count_icount = 0;
if($sub_uls.length) {


2) Provide the base when using parseInt().

Old Code:

count_icount = parseInt($child_a.attr('data-icount'));


New Code:

count_icount = parseInt($child_a.attr('data-icount'), 10);


3) Eliminate commented code sections.

Old code:

var sub_lis = $sub_uls.find('a');
//console.log('Total anchors: '+sub_lis.length);
sub_lis.each(function(index2) {


New Code:

var sub_lis = $sub_uls.find('a');
sub_lis.each(function(index2) {


4) Simplify if conditions by using Truthy &amp; Falsey .

Boolean(undefined); // =&gt; false
Boolean(null); // =&gt; false
Boolean(false); // =&gt; false
Boolean(0); // =&gt; false
Boolean(""); // =&gt; false
Boolean(NaN); // =&gt; false

Boolean(1); // =&gt; true
Boolean([1,2,3]); // =&gt; true
Boolean(function(){}); // =&gt; true


More here    

Old Code:

if(num_sub_uls &gt; 0) {


New Code:

if(num_sub_uls) {


5) Use .append() to append text to an element in jQuery.

Old Code:

var a_txt = $child_a.text();
$child_a.text(a_txt+' ('+count_icount+') ');


New Code:

$child_a.children('a').append( ' (' + count_icount + ') ' );


6) Simplify your if and else conditions.

Check if count_icount is greater than 0, instead of checking for sub_list.length;

Old Code:

if(num_sub_uls &gt; 0) {
    var sub_lis = $sub_uls.find('a');
    sub_lis.each(function(index2) {
        count_icount += parseInt($(this).attr('data-icount'));
    });
} else {
    count_icount = parseInt($child_a.attr('data-icount'));
}


New Code:

$sub_uls.find('a').each(function(index2) {
    count_icount += parseInt($(this).attr('data-icount'));
});
if( !count_icount ){
    count_icount = parseInt($this.children('a').attr('data-icount'), 10);
}


7) Use .data() instead of .attr() to access properties that begin with data-.

.data() Documentation

Example:

&lt;a id="thing2" data-icount="9" href="/some/thing/2" &gt;Fruits&lt;/a&gt;


Use 

$("#thing2").data( "icount" ) == "9";


Instead of  

$("#thing2").attr( "data-icount" ) == "9";


Final code:

$(function () {
    $("#tree li").each(function () {
        var $this = $(this),
        count_icount = $this.find("a").data('icount');

        $this.children('ul').find('a').each(function () {
            count_icount += parseInt($(this).data('icount'), 10);
        });
        if (!count_icount) {
            count_icount = parseInt($this.children('a').data('icount'), 10);
        }
        $this.children('a').append(' (' + count_icount + ') ');
    });
});


Demo: http://jsfiddle.net/GQkB8/2/