Idea: Store the current node in the array, if the current node is a leaf node to print the current array, take the recursive way to operate.
Technique One: is the problem of the array, all the path nodes are stored in an array, because the array transfer is the address, change the array point to the content of the other pass the current array of methods will also be affected, so in the parameters also need a shape of the variable to control the current length of the array, When printing, just print this length of the array so you can make a difference.
technique Two: can make this kind of modification operation is because the program is the linear execution does not produce the data error question, once if the parallel printing will appear the data many times changes the error
Here is the implementation code:
Print all paths
//parameters for all current trees: root node, storage node array, control path length shaping variable
/hint: Shape variable size is important here. Public
void Printway (Node root, String[] Path,int Size {
//If the root node is empty direct returns if
(root = null) {return
;
}
The root node is not stored in the array as null
path[size++] = Root.getdata ();
Make logical judgments to see if the current node is a leaf node. If it is a leaf node, print the current array in length for size if
(Root.getleftnode () ==null&&root.getrightnode () = = NULL) {
System.out.println ("path" + (++treepath) + "to");
for (int i=0;i<size;i++) {
System.out.print (path[i]+ "");
}
System.out.println ();
If the leaf node is not recursive go in and continue to interpolate into the array
}else{
printway (Root.getleftnode (), path,size);
Printway (Root.getrightnode (), path,size);
}
This is how all the paths are printed. All two-fork tree operation method Please refer to another article
http://blog.csdn.net/u014494703/article/details/50989223