首页 > 其他 > 详细

tree 中 最可能大的路径

时间:2014-02-25 22:13:40      阅读:272      评论:0      收藏:0      [点我收藏+]
bubuko.com,布布扣
public class Solution1 {
    

    int sum = 0;
    public int hightvalue(Node root){
        if(root == null)
            return 0;
        if(root.left == null && root.right == null)
            return 1;
        int left = hightvalue(root.left);
        int right = hightvalue(root.right);
        sum = sum < (left+right) ? (left+right) : sum;
        int ret = Math.max(left,right) + 1;
        return ret;
    }
    
    public static void main(String[] args){
      
        Node N7 = new Node(7,null,null);
        Node N6 = new Node(6,N7,null);
        Node N5 = new Node(5,null,N6);
        Node N4 = new Node(4,null,null);
        Node N2 = new Node(2,N4,N5);
        Node N3 = new Node(3,null,null);
        Node N1 = new Node(1,N2,N3);
        Solution1 s = new Solution1();
        System.out.println(s.hightvalue(N1));
        System.out.println(s.sum);
        
    }
    
}

class Node{
    int val;
    Node left;
    Node right;
    Node(int val, Node left, Node right){
        this.val = val;
        this.left = left;
        this.right = right;
    }
}
bubuko.com,布布扣

tree 中 最可能大的路径

原文:http://www.cnblogs.com/leetcode/p/3566311.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!