Print binary tree all paths---JAVA implementation __java

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.