"ZTE Holding the Moon" competition--the shortest path of two-fork search tree (BST) tree Java Solution

Source: Internet
Author: User

Problem Description:

The BST tree, also called the binary search tree, finds the minimum value of the path to all leaf nodes

Test Case One: 10

5 20

Returns 15;

Test Case TWO: 100

20 70 110 120

NULL null-NULL NULL-NULL

Returns 130;

Program code implementation:

  

1  PackageExamination.written;2 3 /**4  *5  * @authorZhuxy6 * @time 2016-6-12 pm 9:57:537  *8  */9  Public classBstminlength {Ten  One      Public Static voidMain (string[] args) { A  -         /* - * Root:tnoderoot the * One layer:tnode21 tNode22 - * layer:tnode11 tNode12 tNode13 tNode14 -          */ -          +         //construct a BST tree -TreeNode TNode11 =NewTreeNode (10,NULL,NULL); +TreeNode tNode12 =NewTreeNode (50,NULL,NULL); ATreeNode tNode13 =NewTreeNode (5,NULL,NULL); atTreeNode TNode14 =NewTreeNode (30,NULL,NULL); -  -TreeNode TNODE21 =NewTreeNode (30, TNode11, tNode12); -TreeNode TNode22 =NewTreeNode (30, TNode13, tNode14); -  -TreeNode Tnoderoot =NewTreeNode (100, TNODE21, tNode22); in  - System.out.println (minlength (Tnoderoot)); to  +     } -  the     /** * * Get the shortest path to the BST Tree $      * @paramTnodePanax Notoginseng      * @returnMinlength:bst Shortest Path -      */ the     Private Static intminlength (TreeNode tnode) { +         //TODO auto-generated Method Stub A         if(Tnode! =NULL) { the             returnGetLength (tnode,0); +         } -         return-1; $     } $  -     /** - * Recursive acquisition of the shortest path of a non-empty root node to a leaf node the      * @paramTnode -      * @paramCurlengthWuyi      * @returnminleft>minright? minright:minleft the      */ -     Private Static intGetLength (TreeNode tnode,intcurlength) { Wu         //TODO auto-generated Method Stub -         intMinleft=-1; About         intMinright=-1; $          -         //left dial hand tree -         if(tnode.leftnode!=NULL){ -Minleft=getlength (Tnode.leftnode, curlength+tnode.value); A         } +          the         //Right sub-tree -         if(tnode.rightnode!=NULL) { $Minright=getlength (Tnode.rightnode, curlength+tnode.value); the         } the          the         //leaf node the         if(tnode.leftnode==NULL&& tnode.rightnode==NULL) { -             returncurlength+Tnode.value; in         } the          the         //If the left dial hand tree is empty About         if(tnode.leftnode==NULL) { the             returnMinright; the         } the          +         //if the right sub-tree is empty -         if(tnode.rightnode==NULL) { the             returnMinleft;Bayi         } the          the         //if the right subtree and the left subtree are not empty -         returnMinleft>minright?Minright:minleft; -     } the } the  the /** the * Define the BST tree structure type -  * @authorZhuxy the  * the  */ the classTreeNode {94     intvalue; the TreeNode Leftnode; the TreeNode Rightnode; the 98TreeNode (intvalue, TreeNode lefenode, TreeNode rightnode) { About          This. Value =value; -          This. Leftnode =Lefenode;101          This. Rightnode =Rightnode;102     }103}
View Code

"ZTE Holding the Moon" competition--the shortest path of two-fork search tree (BST) tree Java Solution

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.