class Solution { // 二叉树直径 其实就是根到左子树最深+根到右子树最深 int diameter; public int diameterOfBinaryTree(TreeNode root) { calculateDepth(root); return diameter; } private int calculateDepth(TreeNode node) { if (node == null) { return 0; } int leftDepth = calculateDepth(node.left); int rightDepth = calculateDepth(node.right); diameter = Math.max(diameter, leftDepth + rightDepth); return Math.max(leftDepth, rightDepth) + 1; } }
标签:node,diameter,int,力扣,543,rightDepth,二叉树,calculateDepth From: https://www.cnblogs.com/JavaYuYin/p/18017465