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);
}
...全文
791 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>(); }

58,454

社区成员

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

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