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

 

转载于:https://www.cnblogs.com/leetcode/p/3566311.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值