“中兴捧月”比赛之——二叉查找树(BST)树的最短路径Java求解

问题描述:

 BST树,又称二叉查找树,求其到所有叶子节点路径的最小值

测试用例一:  10

      5  20

  返回15;

测试用例二:   100

    20  70  110  120

  10  null null 89 null null null null

  返回130;

程序代码实现:

  

  1 package examination.written;
  2 
  3 /**
  4  *
  5  * @author ZhuXY  
  6  * @time   2016-6-12 下午9:57:53
  7  *
  8  */
  9 public class BSTMinLength {
 10 
 11     public static void main(String[] args) {
 12 
 13         /*
 14          * root:            tNodeRoot
 15          * one layer: tNode21            tNode22
 16          * two layer:tNode11 tNode12 tNode13 tNode14
 17          */
 18         
 19         //构造一个BST树
 20         TreeNode tNode11 = new TreeNode(10, null, null);
 21         TreeNode tNode12 = new TreeNode(50, null, null);
 22         TreeNode tNode13 = new TreeNode(5, null, null);
 23         TreeNode tNode14 = new TreeNode(30, null, null);
 24 
 25         TreeNode tNode21 = new TreeNode(30, tNode11, tNode12);
 26         TreeNode tNode22 = new TreeNode(30, tNode13, tNode14);
 27 
 28         TreeNode tNodeRoot = new TreeNode(100, tNode21, tNode22);
 29 
 30         System.out.println(minlength(tNodeRoot));
 31 
 32     }
 33 
 34     /**
 35      * 获取BST树的最短路径
 36      * @param tNode
 37      * @return minLength:BST最短路径
 38      */
 39     private static int minlength(TreeNode tNode) {
 40         // TODO Auto-generated method stub
 41         if (tNode != null) {
 42             return getlength(tNode,0);
 43         }
 44         return -1;
 45     }
 46 
 47     /**
 48      * 递归获取非空根节点到叶子节点的最短路径
 49      * @param tNode
 50      * @param curLength
 51      * @return minLeft>minRight? minRight:minLeft
 52      */
 53     private static int getlength(TreeNode tNode,int curLength) {
 54         // TODO Auto-generated method stub
 55         int minLeft=-1;
 56         int minRight=-1;
 57         
 58         // 左子树
 59         if (tNode.leftNode!=null){
 60             minLeft=getlength(tNode.leftNode, curLength+tNode.value);
 61         }
 62         
 63         // 右子树
 64         if (tNode.rightNode!=null) {
 65             minRight=getlength(tNode.rightNode, curLength+tNode.value);
 66         }
 67         
 68         // 叶子节点
 69         if (tNode.leftNode==null && tNode.rightNode==null) {
 70             return curLength+tNode.value;
 71         }
 72         
 73         // 如果左子树为空
 74         if (tNode.leftNode==null) {
 75             return minRight;
 76         }
 77         
 78         // 如果右子树为空
 79         if (tNode.rightNode==null) {
 80             return minLeft;
 81         }
 82         
 83         // 如果右子树和左子树都不为空
 84         return minLeft>minRight? minRight:minLeft;
 85     }
 86 }
 87 
 88 /**
 89  * 定义BST树结构类型
 90  * @author ZhuXY
 91  *
 92  */
 93 class TreeNode {
 94     int value;
 95     TreeNode leftNode;
 96     TreeNode rightNode;
 97 
 98     TreeNode(int value, TreeNode lefeNode, TreeNode rightNode) {
 99         this.value = value;
100         this.leftNode = lefeNode;
101         this.rightNode = rightNode;
102     }
103 }
View Code

 

posted @ 2016-06-12 23:02  YouxiBug  阅读(1660)  评论(0编辑  收藏  举报