递归算法

 private JSONArray queryTree(Long parentId, List<Long> grantedList) {
                JSONArray array = new JSONArray();
                List<City> cityList = cityService.loadByParentId(parentId);
                  for(City city : cityList) {
                  JSONObject obj = new JSONObject();
                  obj.put("text", city.getCityName());
                  obj.put("cityId", city.getCityId());
                  obj.put("iconCls", "tree_node_noicon");
                  if(city.getLevelNum() < 3) {
                  obj.put("expanded", true);
                  }
                  obj.put("checked", grantedList.contains(city.getCityId()));
                  JSONArray children = queryTree(city.getCityId(), grantedList);
                  if(children.length() == 0) {
                  obj.put("leaf", true);
                  } else {
                  obj.put("children", children);
                  }
                  array.put(obj);
                  }
                  return array;
 }

你可能感兴趣的:(算法)