The problem of maximum distance between two fork tree nodes __ Two fork Tree

Source: Internet
Author: User

The maximum distance problem between two fork tree nodes public class maxdistanceoftree{//two fork tree node definition public static class node{public int value;
		Public Node left;

		public Node right;
		public Node (int data) {this.value=data;  }//Get the maximum distance between two nodes of a binary tree the length of/** maximum subtree is: Zoozi tree maximum length + right subtree maximum length +1/public static int getmaxdistance (Node head) {int  []num=new int[1];
        The length of the node used to record each subtree//system.out.println (num[0]);	
    Return Posorder (Head,num);
			}//The sequence of the binary tree traverses the public static int Posorder (Node head,int []num) {if (head==null) {num[0]=0;
		return 0;
        ///Recursive call Zoozi tree int lmax=posorder (head.left,num);
        int maxfromleft=num[0];
        System.out.println (Maxfromleft);
        Recursive call right subtree int rmax=posorder (head.right,num);
        int maxfromright=num[0]; int curnodemax=maxfromleft+maxfromright+1;
        The maximum length of the left and right subtree is added 1 Num[0]=math.max (maxfromleft,maxfromright) +1;

	Return Math.max (Math.max (Lmax,rmax), Curnodemax); public static void Main (string[]args) {/** constructed two-forked tree 1 2 3 4 5 6 7
		8 */Node Head=new node (1);
		Head.left=new Node (2);
		Head.right=new Node (3);
		Head.left.left=new Node (4);
		Head.left.right=new Node (5);
		Head.right.left=new Node (6);
		Head.right.right=new Node (7);
        Head.right.right.left=new Node (8);

	The maximum distance System.out.println (getmaxdistance head) between nodes of two fork tree is obtained. }
}


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.