tree 中 最可能大的路径

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;
    }
}

 

posted on 2014-02-25 11:29  brave_bo  阅读(230)  评论(0编辑  收藏  举报

导航