Exception in thread "main" java.lang.StackOverflowError!!!错误,搞到凌晨三点没找到错误在哪,真心求指教

v_李大仁_v 2014-10-24 02:44:56
public void print(Node root,int one,List<Integer> path,int tmpsum){
if(this.root==null){
return;
}

tmpsum += root.value;
path.add(this.root.value);//报错指的这一行(共两行)

boolean leaf = (root.left==null&&root.right==null);

if(leaf&&tmpsum==one){
for(int i:path){
System.out.print(i+"->");
}
System.out.println("end");
}

print(this.root.left,one,path,tmpsum);//报错指的这一行(共两行)
print(this.root.right,one,path,tmpsum);

path.remove(path.size()-1);
}
...全文
866 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
v_李大仁_v 2014-10-24
  • 打赏
  • 举报
回复
非常感谢你们的回复,我又学到了一些知识
tony4geek 2014-10-24
  • 打赏
  • 举报
回复
参考楼上的。
shixitong 2014-10-24
  • 打赏
  • 举报
回复
把public void print(Node root,int one,List<Integer> path,int tmpsum)方法中的this去掉 this.root是指当前访问对象的root,而实际我们要用到的这个方法中的root 如果是this.root这样总是取跟节点就死循环了
public void print(Node root,int one,List<Integer> path,int tmpsum){
        if(root==null){
            return;
        }
        tmpsum += root.value;
        path.add(root.value);

        boolean leaf = (root.left==null&&root.right==null);

        if(leaf&&tmpsum==one){
            for(int i:path){
                System.out.print(i+"->");
            }
            System.out.println("end");
        }

        print(root.left,one,path,tmpsum);
        print(root.right,one,path,tmpsum);

        path.remove(path.size()-1);
    }
v_李大仁_v 2014-10-24
  • 打赏
  • 举报
回复
//所有的代码 import java.util.ArrayList; import java.util.List; public class BinaryTree { class Node{ int value; Node left; Node right; Node(int value){ this.value = value; left=null; right=null; } public Node getLeft() { return root.left; } public Node getRight(){ return root.right; } } private Node root; public BinaryTree() { root = null; } public void insert(int value){ root = insert(root,value); } public Node insert(Node root,int value){ if(root==null){ Node nRoot = new Node(value); root = nRoot; return root; } if(root.value<value){ root.right = insert(root.right,value); }else if(root.value>value){ root.left = insert(root.left,value); }else if(root.value==value){ System.out.println("The num is repeats"); }else{ root.value = value; } return root; } public void delete(int value){ root = delete(root,value); } public Node delete(Node root,int value){ return root; } public void inOrderPrient(){ inOrderPrient(root); } public void inOrderPrient(Node root){ if(root==null){ return; } //System.out.println(root.value); inOrderPrient(root.left); inOrderPrient(root.right); System.out.println(root.value); } public void print(int value){ print(this.root,value,new ArrayList<Integer>(),0); } public void print(Node root,int one,List<Integer> path,int tmpsum){ if(this.root==null){ return; } tmpsum += root.value; path.add(this.root.value); boolean leaf = (root.left==null&&root.right==null); if(leaf&&tmpsum==one){ for(int i:path){ System.out.print(i+"->"); } System.out.println("end"); } print(this.root.left,one,path,tmpsum); print(this.root.right,one,path,tmpsum); path.remove(path.size()-1); } public static void main(String[] args) { ArrayList<Integer> arr = new ArrayList<Integer>(); BinaryTree bt = new BinaryTree(); bt.insert(10); bt.insert(5); bt.insert(4); bt.insert(7); bt.insert(12); bt.print(22); //bt.insert(10); ///bt.inOrderPrient(); } //private static int sum = 0; //private static ArrayList<Integer> arr = new ArrayList<Integer>(); }
内容概要:本文围绕“多主体综合能源系统+分布式ADMM研究”展开,系统阐述了交替方向乘子法(ADMM)在多主体综合能源系统中的分布式优化应用,提供了完整的Matlab代码实现。研究聚焦于多利益主体之间的协同优化问题,通过建立合理的数学模型,采用ADMM算法实现去中心化的解架构,有效保障各主体的数据隐私性与决策自主性,同时提升整体系统的计算效率与优化性能。内容涵盖系统建模、优化问题分解、ADMM迭代解流程、收敛性验证及结果分析,具有较强的理论深度与工程实践价值,适用于能源互联网、微电网群、园区综合能源系统等复杂场景的能量管理与协调调度。; 适合人群:具备电力系统分析、优化理论基础及Matlab编程能力的研究生、高校科研人员,以及从事综合能源系统规划、微电网优化、分布式能源管理等方向的工程技术人员。; 使用场景及目标:①应用于多主体参与的综合能源系统能量管理与协同优化调度;②帮助读者深入理解ADMM算法的分解-协调机制及其在分布式优化中的具体实现,支撑高水平学术论文复现、科研课题攻关或实际工程项目开发;③推动智能电网与能源互联网中去中心化、隐私保护型优化调度技术的发展与落地; 阅读建议:建议读者结合文中提供的Matlab代码进行动手实践,逐行调试并可视化关键变量的迭代过程,深入掌握ADMM算法的参数设置、收敛判据及数值稳定性处理技巧,并尝试将其拓展至电动汽车集群调度、多微电网互动、可再生能源消纳等其他分布式优化应用场景中进行仿真验证。

58,440

社区成员

发帖
与我相关
我的任务
社区描述
Java Eclipse
社区管理员
  • Eclipse
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧