题解 | #二叉树的直径#

二叉树的直径

http://www.nowcoder.com/practice/15f977cedc5a4ffa8f03a3433d18650d

import java.util.*;

public class Solution {
    
    public int num;
    
    public int dfs(TreeNode root){
        if(root == null)return -1;
        if(root.left == null && root.right == null)return 0;
        int x1 = dfs(root.left)+1;
        int x2 = dfs(root.right)+1;
        
        num = Math.max(num,x1+x2);
        
        return Math.max(x1,x2);
    }
    
    public int diameterOfBinaryTree (TreeNode root) {
        // write code here
        if(root == null)return 0;
        dfs(root);
        return num;
    }
}
全部评论

相关推荐

不愿透露姓名的神秘牛友
07-02 18:35
简历上把1个月实习写成了3个月,会进行背调吗?
码农索隆:一个月有一个月的实习经历,三个月有三个月的实习经历
点赞 评论 收藏
分享
05-12 22:16
已编辑
北京邮电大学 研发工程师
牛客30236098...:0offer+1 滴滴都不给我面 佬没投鹅吗,鹅应该很喜欢北邮吧
投递美团等公司10个岗位
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务