树形结构 求出总子树的个数

有一个三叉树形结构
每一个节点都有左 中 右三个子节点
子节点里面有fatherId 即它的父节点
现在给一个节点 求出它的所有子节点个数。
我现在是这样做的 求优化。
private int findCountByCode(String Code) {
int count=0;
List users= userService.queryList("from UserInfo where fatherID=?", loginCode);
count=users.size();
for(UserInfo user:users){
count=count+findCountByLoginCode(user.getLoginCode());
}
return count;

}

可以建立自这个表自己和自己的多对多关联来查询

private void getDeleteIds(Long id, List ids) {
// 跟据父ID查询子节点
ContentCategory category = new ContentCategory();
category.setParentId(id);
List list = super.queryListByWhere(category);
// 找到了子节点
if (list != null && list.size() > 0) {
for (ContentCategory contentCategory : list) {
// 把子节点ID返回
ids.add(contentCategory.getId());

            // 如果当前循环节点是个父节点
            if (contentCategory.getIsParent()) {
                // 递归查询所有子节点
                getDeleteIds(contentCategory.getId(), ids);
            }
        }
    }
}

最后得到list的长度就可以了