将如下递归代码用非递归方式实现?

    public void getTreeChildren(JSONArray tree,JSONArray listAll,String parentId){
        for(int i=0;i<tree.size();i++){
            JSONObject node=tree.getJSONObject(i);
            JSONArray children=new JSONArray();
            
            for (int j = 0; j < listAll.size(); j++) {
                JSONObject jsonObject=listAll.getJSONObject(j);
                if (jsonObject.getInt(parentId)==node.getInt("id")) {
                    children.add(jsonObject);
                }
            }
            if (children.size()>0) {
                node.put("spread", true);//展开子节点
                node.put("children", children);
                getTreeChildren(node.getJSONArray("children"),listAll,parentId);
            }
            
        }
    }
查看回复