public void findSum(TreeNode node,int sum){
int depth=depth(node);
int[] path=new int[depth];
findSum(node,sum,path,0);
}
public void findSum(TreeNode node,int sum,int[] path,int level){
if(node==null){
return;
}
path[level]=node.data;//将当前结点插入路径
int t=0;
for(int i=level;i>=0;i--){
t+=path[level];
if(t==sum){
print(path,i,level);
}
} //查找以当前结点为终点且综合为sum的路径
findSum(node.left,sum,path,level+1);
findSum(node.right,sum,path,level+1);//查找此节点之下的结点
}
path[level]=Integer.MIN_VALUE;//从路径中移除当前结点
public void print(int[] path,int start,int end){
for(int i=start;i<=end;i++){
System.out.println(path[i]+' ');
}
System.out.println();
}
public int depth(TreeNode node){
if(node==null){
return 0;
}
else{
return 1+Math.max(depth(node.left),depth(node.right));
}
}//求二叉树中和为某一值得路径,起点不一定是根节点或者叶子结点
int depth=depth(node);
int[] path=new int[depth];
findSum(node,sum,path,0);
}
public void findSum(TreeNode node,int sum,int[] path,int level){
if(node==null){
return;
}
path[level]=node.data;//将当前结点插入路径
int t=0;
for(int i=level;i>=0;i--){
t+=path[level];
if(t==sum){
print(path,i,level);
}
} //查找以当前结点为终点且综合为sum的路径
findSum(node.left,sum,path,level+1);
findSum(node.right,sum,path,level+1);//查找此节点之下的结点
}
path[level]=Integer.MIN_VALUE;//从路径中移除当前结点
public void print(int[] path,int start,int end){
for(int i=start;i<=end;i++){
System.out.println(path[i]+' ');
}
System.out.println();
}
public int depth(TreeNode node){
if(node==null){
return 0;
}
else{
return 1+Math.max(depth(node.left),depth(node.right));
}
}//求二叉树中和为某一值得路径,起点不一定是根节点或者叶子结点