The beauty of programming 3.8 finding the maximum distance of a node in a binary tree

Source: Internet
Author: User

Description: If the two-fork tree is viewed as a graph, the connection between parent-child nodes is considered bidirectional, and the number of edges between two nodes is defined as "distance". The distance between the two nodes farthest apart in the binary tree is obtained.

Idea: The two nodes farthest from each other must be leaf nodes, and the paths of the two leaf nodes have two conditions:

1. The path passes through the root node, then two leaf nodes belong to Root.left and root.right as the root of the subtree, and is two sub-trees in the distance root.left and root.right the farthest leaf node;

2. The path does not go through the root node, then the two leaf nodes on the root.left or root.right;

According to the above analysis, referring to the "programming beauty" given in the C language code, here gives the implementation of Java.

 Public classMaxdistanceofbinarytree_3_8 {intMaxLen = 0;  Public voidfindmaxdistance (Node root) {if(Root = =NULL)            return; if(Root.left! =NULL) {findmaxdistance (root.left); } Else{root.maxleft= 0; }                if(Root.right! =NULL) {findmaxdistance (root.right); } Else{root.maxright= 0; }                if(Root.left! =NULL) {            intTMP =0; if(Root.left.maxLeft >root.left.maxRight) {tmp=Root.left.maxLeft; } Else{tmp=Root.left.maxRight; } root.maxleft= tmp + 1; }                if(Root.right! =NULL) {            intTMP = 0; if(Root.right.maxLeft >root.right.maxRight) {tmp=Root.right.maxLeft; } Else{tmp=Root.right.maxRight; } root.maxright= tmp +1; }        if(Root.maxleft + root.maxright >maxlen) {MaxLen= Root.maxleft +Root.maxright; }    }             Public Static voidMain (string[] args) {Node N1=NewNode (1); Node N2=NewNode (2); Node N3=NewNode (3); Node N4=NewNode (4); Node N5=NewNode (5); Node N6=NewNode (6); Node N7=NewNode (7); Node N8=NewNode (8); Node N9=NewNode (9); N1.left= N2; N1.right =N3; N2.left= N4; N2.right =N5; N4.left=N6; N5.right=N7; N6.left=N8; N7.right=N9; Newmaxdistanceofbinarytree_3_8 (). Findmaxdistance (N1); }}classNode { Public intVal;  PublicNode left;//left dial hand tree     PublicNode right;//Right sub-tree     Public intMaxleft;//maximum distance from Zuozi to leaf node     Public intMaxright;//the maximum distance from the right subtree to the leaf node     PublicNode (intval) {         This. val =Val; }}

The above code introduces two additional parameters, Maxleft and maxrigth, in the definition of node, which does not exist in the generic two-fork tree node definition, and also has an additional global traversal maxlen in the above code;

In these two aspects of the consideration, modify the above code, and get the modified code as follows:

 Public classMaxdistanceofbinarytree { PublicResult findmaxdistance (Node root) {if(Root = =NULL)            return NewResult (0,-1); Result Left=findmaxdistance (Root.left); Result Right=findmaxdistance (root.right); intMaxdis = LEFT.MAXDEP + RIGHT.MAXDEP + 2; Maxdis=Math.max (Maxdis, Math.max (Left.maxdis, Right.maxdis)); return NewResult (Maxdis, Math.max (LEFT.MAXDEP, RIGHT.MAXDEP) +1); }         Public Static voidMain (string[] args) {Node N1=NewNode (1); Node N2=NewNode (2); Node N3=NewNode (3); Node N4=NewNode (4); Node N5=NewNode (5); Node N6=NewNode (6); Node N7=NewNode (7); Node N8=NewNode (8); Node N9=NewNode (9); N1.left= N2; N1.right =N3; N2.left= N4; N2.right =N5; N4.left=N6; N5.right=N7; N6.left=N8; N7.right=N9; Result R=NewMaxdistanceofbinarytree (). Findmaxdistance (N1);    System.out.println (R.maxdis); }}classresult{ Public intMaxdis;//maximum distance rooted at root     Public intMAXDEP;//The longest distance from root to Leaf     PublicResult (intMaxdis,intMAXDEP) {         This. Maxdis =Maxdis;  This. MAXDEP =MAXDEP; }}classNode { Public intVal;  PublicNode left;//left dial hand tree     PublicNode right;//Right sub-tree     PublicNode (intval) {         This. val =Val; }}

In the modified code, a new class result is introduced that holds the results of recursive root.left and root.right computations.

For more detailed instructions, refer to: http://www.cnblogs.com/miloyip/archive/2010/02/25/1673114.html

The beauty of programming 3.8 finding the maximum distance of a node in a binary tree

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.